简体   繁体   English

用随机数初始化一个 const 数组

[英]Initialize a const array with random numbers

I have been tasked to calculate the time it takes for my PC to do a matrix multiplication with dimensions 2048x2048 (and get 10 samples) and I was given the following function我的任务是计算我的 PC 进行尺寸为 2048x2048 的矩阵乘法(并获得 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'

and this to use in my main这在我的主要使用

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: %f\n", it, time);
}

So what I have to do is initialize the arrays A and B with random values?那么我要做的是用随机值初始化数组 A 和 B 吗?
I can't think of a way.我想不出办法。
This is what I have so far这是我到目前为止所拥有的

#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: %f\n", 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; are global pointers.是全局指针。 Each, at program start, are given a value 0, a null pointer .在程序开始时,每个都被赋予一个值 0,一个空指针 Since they are const , the pointer cannot change.由于它们是const ,因此指针无法更改。 We are stuck.我们被卡住。


Instead, do not make them const - there is no need for that.相反,不要将它们const - 没有必要这样做。 In main() , allocate memory and values before the test.main()中,在测试之前分配内存和值。

Note that matrixMult() uses a pointer to float rather than a more 2D like type.请注意, matrixMult()使用指向float的指针,而不是更像 2D 的类型。 We can take advantage of that when first assigning A, B .我们可以在第一次分配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;
}

Notes: #define sub2ind(i,j,n) (j) +(i)*(n) risks precedence issues.注意: #define sub2ind(i,j,n) (j) +(i)*(n)有优先级问题的风险。 Better as #define sub2ind(i,j,n) ((j) +(i)*(n)) .不如#define sub2ind(i,j,n) ((j) +(i)*(n))更好。 Even better as #define sub2ind(i,j,n) (j) +(size_t)(i)*(n) to insure size_t math as int math is more likely will to overflow.更好的是#define sub2ind(i,j,n) (j) +(size_t)(i)*(n)以确保size_t数学,因为int数学更有可能溢出。

I would reccoment for you to check out the standard function rand()我建议您查看标准函数 rand()

int rand( void )内特兰德(无效)

It returns with a random intiger between 0 to RAND_MAX.它返回一个介于 0 到 RAND_MAX 之间的随机整数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM