简单的C矩阵乘法,多线程
Simple C matrix multiplication, multi thread
我有一个标准的 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
我有一个标准的 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