C语言 用随机数初始化const数组



我的任务是计算我的PC执行尺寸为2048 × 2048的矩阵乘法(并获得10个样本)所需的时间,并给出以下函数

/*
* matrixMult - Matrix multiplication
*/
void matrixMult(float *const C, /* output matrix */
float const *const A, /* first matrix */
float const *const B, /* second matrix */
int const n) { /* number of rows/cols */
for (int i = 0; i < n; i++) { /* rows */
for (int j = 0; j < n; j++) { /* cols */
/* initialize output value */
C[sub2ind(i, j, n)] = 0;
for (int k = 0; k < n; k++) { /* accumulate products */
C[sub2ind(i, j, n)] += A[sub2ind(i, k, n)] * B[sub2ind(k, j, n)];
}
}
}
} // end function 'matrixMult'

和这个在我的主

中使用
double time = 0.0;
/* compute matrix multiplication */
for (int it = 0; it < MAX_ITER; it++) {
gettimeofday(&start, NULL);
matrixMult( C, A, B, n );
gettimeofday(&end, NULL);
time = ( (end.tv_sec - start.tv_sec) * 1000.0 + /* sec to ms */
(end.tv_usec - start.tv_usec) / 1000.0 ); /* us to ms */
fprintf("Iter: %d Time: %fn", it, time);
}

那么我要做的就是用随机值初始化数组A和B ?
我想不出一个办法。
这是我目前看到的

#include <stdio.h>
#include <time.h>
#include <math.h>
#include <stdlib.h>
#define sub2ind(i,j,n) (j) +(i)*(n)
float *const A, *const B, *const C;
void matrixMult(float *const C, /* output matrix */
float const *const A, /* first matrix */
float const *const B, /* second matrix */
int const n);
int main() {
int n = 2048;
double time = 0.0;
struct timeval start, end;
/* compute matrix multiplication */
for (int it = 0; it < 10; it++) {
gettimeofday(&start, NULL);
matrixMult(C, A, B, n);
gettimeofday(&end, NULL);
time = ((end.tv_sec - start.tv_sec) * 1000.0 + /* sec to ms */
(end.tv_usec - start.tv_usec) / 1000.0); /* us to ms */
fprintf("Iter: %d Time: %fn", it + 1, time);
}
return 0;
}
/*
* matrixMult - Matrix multiplication
*/
void matrixMult(float *const C, /* output matrix */
float const *const A, /* first matrix */
float const *const B, /* second matrix */
int const n) { /* number of rows/cols */
for (int i = 0; i < n; i++) { /* rows */
for (int j = 0; j < n; j++) { /* cols */
/* initialize output value */
C[sub2ind(i, j, n)] = 0;
for (int k = 0; k < n; k++) { /* accumulate products */
C[sub2ind(i, j, n)] += A[sub2ind(i, k, n)] * B[sub2ind(k, j, n)];
}
}
}
} // end function 'matrixMult'

float *const A, *const B, *const C;是全局指针。在程序开始时,给每个函数一个值0,一个空指针。因为它们是const,所以指针不能改变。我们被卡住了。


相反,不要使它们成为const-没有必要这样做。在main()中,在测试前分配内存和值。

注意,matrixMult()使用指向float的指针,而不是更像2D的类型。当第一次赋值A, B时,可以利用这一点。

float *A, *B, *C;
int main() {
...
A = malloc(sizeof A[0] * n * n);
B = malloc(sizeof B[0] * n * n);
// Zero fill product array in case multiplication is in error.
// Assigning simplifies debugging.
C = calloc((size_t)n * n, sizeof C[0]);  
// If out-of-memory ...
if (A == NULL || B == NULL || C == NULL) {
// Might as well exit code here with an error message
}
// Fill A[], B[] with something interesting.
for (size_t i = 0; i < (size_t)n * n; i)) {
A[i] = rand();
B[i] = rand();
}

// Test code here.
... 
// Clean up
free(A); A = NULL;
free(B); B = NULL;
free(C); C = NULL;
}

注:#define sub2ind(i,j,n) (j) +(i)*(n)存在优先级问题。不如#define sub2ind(i,j,n) ((j) +(i)*(n))好。更好的是,#define sub2ind(i,j,n) (j) +(size_t)(i)*(n)可以确保size_t的数学,因为int的数学更有可能溢出。

我建议您查看标准函数rand()

int rand(void)

返回一个介于0到RAND_MAX之间的随机整数。

最新更新