具有多线程的C矩阵

时间:2015-12-12 12:05:42

标签: c multithreading pointers matrix pthreads

我需要创建一个代码来使矩阵与多线程相加,但我犯了错误。我必须准确使用“ 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]);
    }
  }
}

2 个答案:

答案 0 :(得分:0)

alk正确陈述:

  

float **float (*)[something] float A[][LDA]的{​​{1}}不同   函数声明的上下文)。

因此,要进行的更改(以便使用的声明与实际数据类型匹配)是:

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];

答案 1 :(得分:-1)

更改

argmain[i].A = A;

argmain[i].A = (float**)A;