0

I need to create a code to make sum of matrix with multithreads, but I make mistakes. I must use precisely that function "matmatthread" without change input params.

I'm incurring in this error:

multimatrix.c:42:24: warning: assignment from incompatible pointer type
[enabled by default]
argmain[i].A = A;

multimatrix.c:43:24: warning: assignment from incompatible pointer type
[enabled by default]
argmain[i].B =B;

multimatrix.c:44:24: warning: assignment from incompatible pointer type
[enabled by default]
argmain[i].C =C;

This is the code:

#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]);
    }
  }
}
alk
  • 69,737
  • 10
  • 105
  • 255
  • [An array of array is not the same as a pointer to pointer](http://stackoverflow.com/questions/18440205/casting-void-to-2d-array-of-int-c/18440456#18440456). – Some programmer dude Dec 12 '15 at 12:40
  • Please how is `matmatthread()` called and how are the arguments defined being passed in? – alk Dec 14 '15 at 06:47

2 Answers2

0

alk correctly stated:

A float ** is not the same as a float (*)[something] which float A[][LDA] is (in the context of the function declaration).

Therefore, the changes to be made (so that the used declaration matches the actual data type) are:

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];
Armali
  • 18,255
  • 14
  • 57
  • 171
-1

Change

argmain[i].A = A;

to

argmain[i].A = (float**)A;
nicomp
  • 4,344
  • 4
  • 27
  • 60
  • 1
    Thanks, man! I did the casting as you told me and now I can compile it; but when I run it, I have a "Segmentation fault (core dumped)" on the last line: C_loc[i][j]=C_loc[i][j]+(A_loc[i][j]+B_loc[i][j]); If I try to printf A[0][0] after the line: argmain[i].A = (float**)A; it works; but If I try to printf argmain[i].A[0][0] it gives me a segmentation fault; How can I solve the problem? – Sasamen Dec 12 '15 at 17:51
  • 1
    Just do not blindly cast around to silence error messages. – alk Dec 13 '15 at 11:39
  • @alk, no one did that. Feel free to help with the secondary question rather than find fault where there is none. – nicomp Dec 13 '15 at 16:28
  • 1
    A `float **` is not the same as a `float (*)[something]` which `float A[][LDA]` is (in the context of the function declaration). – alk Dec 14 '15 at 19:15