我的任務是計算我的 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'
這在我的主要使用
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);
}
那么我要做的是用隨機值初始化陣列 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: %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'
uj5u.com熱心網友回復:
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數學更有可能溢位。
uj5u.com熱心網友回復:
我建議您查看標準函式 rand()
內特蘭德(無效)
它回傳一個介于 0 到 RAND_MAX 之間的隨機整數。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/487779.html
