2017-10-07 74 views
1

我想寫一個超級簡單的C程序的矢量乘 - 加「axpy」算法的整數數據類型。程序輸出執行時間來測量機器的性能。矩陣由隨機數填充。當兩個矩陣大小超過800×800

int benchmark(void) { 
    int N;  /* The matrix size, controlled by user input */ 
    int r, c; /* Row and Column number */ 
    int random; /* Random number to fill the matix */ 
    int a = rand() % 20; /* Scale number to multiply x matrix */ 

    printf("Enter the size(N*N) of the matrices(Maximum 1,000,000)\n"); 
    scanf("%d", &N); 

    if (N > 1000000) { 
     fprintf(stderr, "Size of matrix is too large!\n"); 
     return 0; 
    } 

    /* Initialize and fill the matrix x and y */ 
    int xMatrix[N][N], yMatrix[N][N], resultMatrix[N][N]; 

    /* Compute time */ 
    clock_t t; 

    t = clock(); 

    for (r = 0; r < N; r++) { 
     for (c = 0; c < N; c++) { 
      random = rand() % 100; 
      xMatrix[r][c] = a * random; /* Multiply matrix x with random value a */ 
     } 
    } 
    for (r = 0; r < N; r++) { 
     for (c = 0; c < N; c++) { 
      int random = rand() % 100; 
      yMatrix[r][c] = random; 
     } 
    } 

    /* Add two matrix together */ 
    for (r = 0; r < N; r++) { 
     for (c = 0; c < N; c++) { 
      resultMatrix[r][c] = xMatrix[r][c] + yMatrix[r][c]; 
     } 
    } 

    t = clock() - t; 

    double timeTaken = ((double)t)/CLOCKS_PER_SEC; 
    printf("\n -> Total time : %f seconds\n", timeTaken); 
    printf("\n -> Vector length : %d", N * N); 

} 

用戶控制矩陣的大小。 當N的值小於800時,該程序正常工作。

+1

您可能正在用完堆棧。考慮用malloc動態分配數組。 –

+0

800 * 800 = 640,000。 x4(int的大小)= 3,200,000。這是一個很大的**內存分配作爲本地變量,它可能由於缺乏堆棧空間而失敗。把它放在堆上。 –

回答

3

具有自動存儲(堆棧上)分配的對象的尺寸太大,你得到了一個未定義的行爲,更具體一個堆棧溢出

而應該從堆中分配的對象:

/* Initialize and fill the matix x and y */ 
    int (*xMatrix)[N] = malloc(N * sizeof(*xMatrix)); 
    int (*yMatrix)[N] = malloc(N * sizeof(*yMatrix)); 
    int (*resultMatrix)[N] = malloc(N * sizeof(*resultMatrix)); 

並確認沒有任何由malloc()返回的指針是NULL

下面是修改代碼:

int benchmark(void) { 
    int N;  /* The matrix size, controlled by user input */ 
    int r, c; /* Row and Column number */ 
    int random; /* Random number to fill the matix */ 
    int a = rand() % 20; /* Scale number to multiply x matrix */ 

    printf("Enter the size(N*N) of the matrices (Maximum 1,000,000)\n"); 
    if (scanf("%d", &N) != 1) { 
     fprintf(stderr, "Input error!\n"); 
     return 0; 
    } 

    if (N > 1000000) { 
     fprintf(stderr, "Matrix size is too large!\n"); 
     return 0; 
    } 

    /* Initialize and fill the matrix x and y */ 
    int (*xMatrix)[N] = malloc(N * sizeof(*xMatrix)); 
    int (*yMatrix)[N] = malloc(N * sizeof(*yMatrix)); 
    int (*resultMatrix)[N] = malloc(N * sizeof(*resultMatrix)); 

    if (xMatrix == NULL || yMatrix == NULL || resultMatrix == NULL) { 
     fprintf(stderr, "Memory allocation failed!\n"); 
     free(xMatrix); 
     free(yMatrix); 
     free(resultMatrix); 
     return 0; 
    } 

    /* Compute time */ 
    clock_t t = clock(); 

    for (r = 0; r < N; r++) { 
     for (c = 0; c < N; c++) { 
      random = rand() % 100; 
      xMatrix[r][c] = a * random; /* Multiply matrix x with random value a */ 
     } 
    } 
    for (r = 0; r < N; r++) { 
     for (c = 0; c < N; c++) { 
      random = rand() % 100; 
      yMatrix[r][c] = random; 
     } 
    } 

    /* Add two matrix together */ 
    for (r = 0; r < N; r++) { 
     for (c = 0; c < N; c++) { 
      resultMatrix[r][c] = xMatrix[r][c] + yMatrix[r][c]; 
     } 
    } 

    t = clock() - t; 

    double timeTaken = ((double)t)/CLOCKS_PER_SEC; 
    printf("\n -> Total time : %f seconds\n", timeTaken); 
    printf("\n -> Vector length : %lld", (long long)N * N); 

    free(xMatrix); 
    free(yMatrix); 
    free(resultMatrix); 
    return 0; 
} 

不過請注意,你的計算很簡單,大部分時間很可能在rand()功能度過。

+0

洛爾傾斜的方式,'* xMatrix版權[R] =(INT *)malloc的(N *的sizeof(int)的)'將導致'誤差相信()浪費在蘭特大多數時間 –

+0

[(sizetype)(N)]'int *'' –

+0

@zuolizhu:當然,但是我的帖子中沒有這樣的代碼。 'xMatrix'是'N'' int's指向'N'的二維數組的指針,分配給'malloc()'的一次調用。 – chqrlie

1

您試圖動態分配memmory,我會建議爲以下所示的使用的mallocstdlib.h中

此外,檢查出這些所謂的帖子:memory allocation in Stack and Heap,並What and where are the stack and heap?

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 

int benchmark(void) { 
    int N; /* The matrix size, controlled by user input */ 
    int r, c; /* Row and Column number */ 
    int random; /* Random number to fill the matix */ 
    int a = rand() % 20; /* Scale number to multiply x matrix */ 

    printf("Enter the size(N*N) of the matrixs(Maximum 1,000,000)\n"); 
    scanf("%d", &N); 

    if(N > 1000000) { 
     fprintf(stderr, "Size of matrix is too large!\n"); 
     return 0; 
    } 

    /* Initialize and fill the matix x and y */ 
    int** xMatrix = NULL; 
    int** yMatrix = NULL; 
    int** resultMatrix = NULL; 

    /* Using the heap memory allocation instead of the stack */ 
    xMatrix = (int **) malloc(N * sizeof(int *)); 
    yMatrix = (int **) malloc(N * sizeof(int *)); 
    resultMatrix = (int **) malloc(N * sizeof(int *)); 
    for (r = 0; r < N; r++) { 
     xMatrix[r] = (int *) malloc(N * sizeof(int)); 
     yMatrix[r] = (int *) malloc(N * sizeof(int)); 
     resultMatrix[r] = (int *) malloc(N * sizeof(int)); 
    } 

    /* Compute time */ 
    clock_t t; 

    t = clock(); 

    for (r = 0; r < N; r++) { 
     for (c = 0; c < N; c++) { 
      random = rand() % 100; 
      xMatrix[r][c] = a * random; /* Multiply matix x with random value a */ 
     } 
    } 
    for (r = 0; r < N; r++) { 
     for (c = 0; c < N; c++) { 
      int random = rand() % 100; 
      yMatrix[r][c] = random; 
     } 
    } 

    /* Add two matrix together */ 
    for (r = 0; r < N; r++) { 
     for (c = 0; c < N; c++) { 
      resultMatrix[r][c] = xMatrix[r][c] + yMatrix[r][c]; 
     } 
    } 

    t = clock() - t; 

    double timeTaken = ((double)t)/CLOCKS_PER_SEC; 
    printf("\n -> Total time : %f seconds\n", timeTaken); 
    printf("\n -> Vector length : %d", N*N); 

    /* Always remember to free your allocated memory */ 
    for (r = 0; r < N; r++) { 
     free(xMatrix[r]); 
     free(yMatrix[r]); 
     free(resultMatrix[r]); 
    } 
    free(xMatrix); 
    free(yMatrix); 
    free(resultMatrix); 

} 

int main() { 
    benchmark(); 
    return 0; 
} 
+0

它適合我!非常感謝!分配給輸入時不兼容的類型「INT: –