2017-10-07 54 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: –