title: Matrix Multiplication With Multiple Threads in C
body: <p>I want to create a C program that calculates the multiplication of two N*N matrices by using threads.<br />
  I started this code by referring to <a href="https://stackoverflow.com/questions/26478552/matrix-multiplication-using-multiple-threads">Matrix Multiplication using multiple threads</a> but instead of creating N * N threads for each cell of the resulting matrix, I want to create N threads to do the multiplication concurrently where each row of the result matrix will be computed by a different thread.</p>
  <p>My code looks like this so far:</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  #define N 2
  
  struct v {
      int i;  //Row
      int j;  //Column
  };
  
  int A[N][N] = {{ 1, 2 }, { 3, 4 }};//Matrix 1
  int B[N][N] = {{ 2, 3 }, { 4, 5 }};//Matrix 2
  int C[N][N];  //Resulting Matrix
  
  static void *fnc(void *arg) {
      struct v *data = (struct v *)arg;
      int l;
      for (l = 0; l &lt; N; l++) {
          int i = ((struct v *)data[l]).i;  //Row No
          int j = ((struct v *)data[l]).j;  //Column No
          int accumulator = 0;
          int d, sum = 0;
          for (d = 0; d &lt; N; d++) {
              sum = sum + A[i][d] * B[d][j];
          }
          C[i][j] = sum;
          sum = 0;
      }
      return;
  }
  
  int main() {
      pthread_t threads[N];
      int i, k;
      for (i = 0; i &lt; N; i++) {
          struct v *data[N];
          for (k = 0; k &lt; N; k++) {
              data[k] = (struct v *)malloc(sizeof(struct v));
              data[k].i = i; //assign the row of C for thread to calculate
              data[k].j = k; //assign the column of C for thread to calculate
          }
          //In this example it creates 2 threads with passing data.
          //Data consists of row and column pairs for each thread, that will be calcuting the pairs.
          //Consider first iteration of this loop:
          //Thread 1 is created and data consists of (0,0) and (0,1) which are the targeted calculation cells for thread 1. 
          //In the second iteration: Thread 2 will have (1,0) and (1,1) pairs in its data.
  
          pthread_create(&amp;threads[i], NULL, fnc, data);
      }
      for (i = 0; i &lt; N; i++)
          pthread_join(threads[i], NULL);
  
     for (i = 0; i &lt; N; i++)
        for (k = 0; k &lt; N; k++)
          printf(&quot;%d\t&quot;, C[i][k]);
  
      return 0;
  }
  </code></pre>
  <p>When I compile with</p>
  <pre><code>gcc thread.c -lpthread -lrt
  </code></pre>
  <p>I receive this Error:</p>
  <pre><code>thread.c: In function ‘fnc’:
  thread.c:24:24: error: cannot convert to a pointer type
           int i=((struct v*)data[l]).i;//Row No
                          ^
  thread.c:24:35: error: request for member ‘i’ in something not a structure or union
           int i=((struct v*)data[l]).i;//Row No
                                     ^
  thread.c:25:24: error: cannot convert to a pointer type
           int j=((struct v*)data[l]).j;//Column No
                          ^
  thread.c:25:35: error: request for member ‘j’ in something not a structure or union
           int j=((struct v*)data[l]).j;//Column No
                                     ^
  thread.c: In function ‘main’:
  thread.c:48:20: error: request for member ‘i’ in something not a structure or union
               data[k].i = i; //assign the row of C for thread to calculate
                      ^
  thread.c:49:20: error: request for member ‘j’ in something not a structure or union
               data[k].j = k; //assign the column of C for thread to calculate
  </code></pre>
  <p>I believe the logic behind the code is correct and this sure looks like a simple argument passing problem. I put the whole code because I will be happy if I receive your comments about the problem and also a possible better solution for the desired job.</p>