我需要创建一个代码来使多线程矩阵求和,但是我犯了错误。我必须精确使用该功能“ matmatthread
”,而无需更改输入参数。
我发生此错误:
multimatrix.c:42:24:警告:来自不兼容指针类型的赋值[默认启用]
argmain [i] .A = A;
multimatrix.c:43:24:警告:来自不兼容指针类型的赋值[默认启用]
argmain [i] .B = B;
multimatrix.c:44:24:警告:来自不兼容指针类型的赋值[默认启用]
argmain [i] .C = C;
这是代码:
#include<pthread.h>
#include<stdio.h>
#include<math.h>
struct argtype
{
int id;
int LDA;
int LDB;
int LDC;
int NT;
int N;
int M;
int P;
float **A;
float **B;
float **C;
};
void matmatthread(int LDA, int LDB, int LDC, float A[][LDA], float B[][LDB],
float C[][LDC], int N, int M, int P, int NT)
{
void *thread(void *);
int i, j;
pthread_t tid[4];
struct argtype argmain[4];
for (i = 0; i < NT; i++)
{
argmain[i].id = i;
argmain[i].N = N;
argmain[i].M = M;
argmain[i].P = P;
argmain[i].NT = NT;
argmain[i].LDA = LDA;
argmain[i].LDB = LDB;
argmain[i].LDC = LDC;
argmain[i].A = A;
argmain[i].B = B;
argmain[i].C = C;
}
for (i = 0; i < NT; i++)
{
pthread_create(&tid[i], NULL, thread, &argmain[i]);
}
for (i = 0; i < NT; i++)
{
pthread_join(tid[i], NULL );
}
}
void *thread(void *argmain)
{
struct argtype *argthread;
int id_loc, NT_loc, N_loc, M_loc, P_loc;
float **A_loc, **B_loc, **C_loc;
int LDA_loc, LDB_loc, LDC_loc;
int i, j;
argthread = (struct argtype *) argmain;
id_loc = (*argthread).id;
LDA_loc = (*argthread).LDA;
LDB_loc = (*argthread).LDB;
LDC_loc = (*argthread).LDC;
N_loc = (*argthread).N;
M_loc = (*argthread).M;
P_loc = (*argthread).P;
A_loc = (*argthread).A;
B_loc = (*argthread).B;
C_loc = (*argthread).C;
NT_loc = (*argthread).NT;
for (i = 0; i < N_loc; i++)
{
for (j = 0; j < P_loc; j++)
{
C_loc[i][j] = C_loc[i][j] + (A_loc[i][j] + B_loc[i][j]);
}
}
}
最佳答案
正确地陈述:
float **
与float (*)[something]
是不同的(在
函数声明的上下文)。
因此,要进行的更改(以使使用的声明与实际数据类型匹配)是:
15,17c15,17
< float **A;
< float **B;
< float **C;
---
> void *A;
> void *B;
> void *C;
59d58
< float **A_loc, **B_loc, **C_loc;
69a69
> float (*A_loc)[LDA_loc], (*B_loc)[LDB_loc], (*C_loc)[LDC_loc];
关于c - 多线程C矩阵,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34239648/