#include #include #include #define N 4096 #define FILE_A "matrix_A.bin" #define FILE_B "matrix_B.bin" void load_matrix(const char *filename, double *matrix) { FILE *file = fopen(filename, "rb"); if (!file) { perror("無法讀取檔案"); exit(EXIT_FAILURE); } fread(matrix, sizeof(double), N * N, file); fclose(file); } int main() { double *A = (double *)malloc(N * N * sizeof(double)); double *B = (double *)malloc(N * N * sizeof(double)); double *C = (double *)calloc(N * N, sizeof(double)); if (!A || !B || !C) { perror("記憶體配置失敗"); exit(EXIT_FAILURE); } load_matrix(FILE_A, A); load_matrix(FILE_B, B); double start = omp_get_wtime(); #pragma omp parallel for for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { for (int k = 0; k < N; k++) { C[i * N + j] += A[i * N + k] * B[k * N + j]; } } } double end = omp_get_wtime(); printf("矩陣乘法完成,花費時間: %f 秒\n", end - start); free(A); free(B); free(C); return 0; }