c - 简单的 C 矩阵乘法,多线程
问题描述
我有一个标准的 C 基矩阵乘法代码,我在其中为每一行和每一列对创建一个单独的线程。我得到一个空白屏幕,主程序永远卡住,程序永远不会结束。你能看一下吗?我想先创建所有线程,然后分别加入所有线程。
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define size 10
#define M size
#define K size
#define N size
// int A [M][K] = { {1,4}, {2,5}, {3,6} };
// int B [K][N] = { {8,7,6}, {5,4,3} };
int A [M][K];
int B [K][N] ;
int C [M][N];
struct v {
int i; /* row */
int j; /* column */
};
void *runner(void *param); /* the thread */
void fill_matrix(int matrix[][N], int row, int column)
{
// int (*matrix)[row] = malloc(sizeof(int[row][column]));
for (int i = 0; i < row; i++)
{
for (int j = 0; j < column; j++)
{
matrix[i][j] = (rand() % (100-0+1)) + 1;
}
}
return;
}
int main(int argc, char *argv[]) {
int i,j, count = 0;
// int (*A)[M] = malloc(sizeof(int[M][K]));
// int (*B)[K] = malloc(sizeof(int[K][N]));
// A = (int*)malloc(sizeof(A));
fill_matrix(A,M,K);
fill_matrix(B,K,N);
clock_t start = clock();
// pthread_t thread_array[M*N];
pthread_t thread_array[M*N]; //malloc(M*N * sizeof(pthread_t));
pthread_attr_t attr;
pthread_attr_init(&attr);
int idl[M*N];
for(i = 0; i < M; i++) {
for(j = 0; j < N; j++) {
//Assign a row and column for each thread
struct v *data = (struct v *) malloc(sizeof(struct v));
data->i = i;
data->j = j;
/* Now create the thread passing it data as a parameter */
//pthread_t tid; //Thread ID
//pthread_attr_t attr; //Set of thread attributes
//Get the default attributes
//pthread_attr_init(&attr);
//Create the thread
idl[i]=count;
pthread_create(count,&thread_array[count],&attr,runner,data);
//Make sure the parent waits for all thread to complete
//pthread_join(tid, NULL);
count++;
}
}
for(int k =0;k<M*N;k++){
pthread_join(&thread_array[k],NULL);
}
free(thread_array);
clock_t end = clock();
printf("%f\n",(float)(end-start)/CLOCKS_PER_SEC );
//Print out the resulting matrix
//for(i = 0; i < M; i++) {
// for(j = 0; j < N; j++) {
// printf("%d ", C[i][j]);
// }
// printf("\n");
// }
}
//The thread will begin control in this function
void *runner(void *param) {
struct v *data = param; // the structure that holds our data
int n, sum = 0; //the counter and sum
//Row multiplied by column
for(n = 0; n< K; n++){
sum += A[data->i][n] * B[n][data->j];
}
//assign the sum to its coordinate
C[data->i][data->j] = sum;
//Exit the thread
pthread_exit(0);
}
请忽略代码中的注释,我知道有很多。
解决方案
删除free(thread_array);
,你没有malloc
,所以你不需要free
它。
pthread_join(&thread_array[k],NULL);
应该是pthread_join(thread_array[k],NULL);
,因为它采用的是pthread_t
而不是指向它的指针。
里面的第一个参数pthread_create(count,&thread_array[count],&attr,runner,data);
是错误的,删除它 ==> pthread_create(&thread_array[count],&attr,runner,data);
。
你不使用isl
数组,所以删除它。
之后,它至少会编译、运行并给出输出:https ://ideone.com/1r0e2V
推荐阅读
- php - 将记录绑定到另一个实体
- react-native - console.error:"fontFamily "layar-bahtera-regular" 不是系统字体,还没有通过 Font.loadAsync 加载
- javascript - 如何将我的 json 对象插入 Google 图表行
- python - 存储要以加密形式存储的原始未加密文件的 sha256 哈希是否安全?
- wordpress - 过滤 Gutenberg 块编辑器控件
- google-chrome-devtools - puppeteer 中的性能分析
- unix - 将代码行转换为 usb 上 archlinux 的脚本(dos2unix 和 $HOME 不起作用)
- excel - 以 Excel 表格格式从 Excel 复制并粘贴到 Power Point(“保留源格式”)
- flutter - 我们如何使用 SHA256 或在 iOS 和 Android 中的 Flutter 中从相同的字符串生成相同的哈希码?
- ionic4 - 尝试使用 ngIf 显示离子标签