473,657 Members | 2,423 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Matrix Multiplication using multi-threads gets segmentation fault

3 New Member
Hello, I'm trying to write a program that calculates up to a 1024 x 1024 matrix using multi-threading. For example, I need to run a 1024 x 1024 using 256, 64, 16 or 4 threads. Or I need to run a 64 x 64 matrix using 16 or 4 threads. All the Matrices are square. I thought I coded my program correctly, however I get a segmentation fault when I use a 720 x 720 matrix or higher, heres the code.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <pthread.h>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. const int   DIM = 720; //works up to 719, crashes at 720
  9. const int   num_of_thr = 4;
  10. int         matrix_A[DIM][DIM];
  11. int         matrix_B[DIM][DIM];
  12. int         c[DIM][DIM];
  13.  
  14. struct v
  15. {
  16.     int i;
  17.     int j;
  18. };
  19.  
  20. //worker thread
  21. void* matrix_multi(void* data)
  22. {    
  23.     for(int i = 0; i < DIM; i++)
  24.     {
  25.         for(int j = 0; j < DIM; j++)
  26.         {
  27.             c[i][j] = 0;
  28.             for(int k = 0; k < DIM; k++)
  29.             {
  30.                 c[i][j] += matrix_A[i][k] * matrix_B[k][j];
  31.             }
  32.         }
  33.     }
  34.     pthread_exit(0);
  35. }
  36.  
  37. int main()
  38. {
  39.  
  40.     pthread_t thr_id[DIM][DIM];
  41.     pthread_attr_t thr_attr;
  42.     pthread_attr_init(&thr_attr);
  43.  
  44.  
  45.  
  46.    //Filling the Matrices
  47.     for(int i = 0; i < DIM; i++)
  48.     {
  49.         for(int j = 0; j < DIM; j++)
  50.         {
  51.             matrix_A[i][j]= i + j;
  52.             matrix_B[i][j] = i + 3;
  53.         }
  54.     }
  55.  
  56.  
  57.     //create the threads
  58.     for(int i = 0; i < num_of_thr/2; i++)
  59.     {
  60.         for(int j = 0; j < num_of_thr/2; j++)
  61.         {
  62.             struct v *data = (struct v *) malloc(sizeof(struct v));
  63.             data->i = i;
  64.             data->j = j;
  65.             pthread_create(&thr_id[i][j],NULL,matrix_multi,  &data);
  66.         }
  67.     }
  68.  
  69.     //joining the threads
  70.     for(int i = 0; i < num_of_thr/2; i++)
  71.     {
  72.         for(int j = 0; j < num_of_thr/2; j++)
  73.         {
  74.         pthread_join(thr_id[i][j],NULL);
  75.         }
  76.     }
  77.  
  78.      return 0;
  79. }
  80.  
  81.  
  82.  
Any help would be appreciated, thanks in advance.
Sep 15 '10 #1
4 6648
ashitpro
542 Recognized Expert Contributor
I just ran this code on my CentOS machine (g++ 4.1.2).
It worked pretty well, no seg fault. Even if I increase the number of threads and dimensions.
Sep 15 '10 #2
newb16
687 Contributor
Seems that your compiler doesn't like huge variables in data segment (matrix_a) and/or stack (thr_id). Allocate them on the heap. (btw - why do you need thr_id be of size [DIM][DIM] when you only fill it to num_of_thr/2 in each dimension?
Sep 15 '10 #3
Tosh2pointO
3 New Member
Thanks for the quick help. I changed my code quite a bit since yesterday. However I now get a "invalid conversion from `void*' to `__pthread_t**" on line 63 of my code. Here is the updated code:
Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <pthread.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5.  
  6. #define SIZE 10            /* Size of matrices */
  7. int N;                /* number of threads */
  8.  
  9. int A[SIZE][SIZE], B[SIZE][SIZE], C[SIZE][SIZE];
  10.  
  11. void fill_matrix(int m[SIZE][SIZE])
  12. {
  13.   int i, j, n = 0;
  14.   for (i=0; i<SIZE; i++)
  15.     for (j=0; j<SIZE; j++)
  16.       m[i][j] = n++;
  17. }
  18.  
  19. void print_matrix(int m[SIZE][SIZE])
  20. {
  21.   int i, j = 0;
  22.   for (i=0; i<SIZE; i++) {
  23.     printf("\n\t| ");
  24.     for (j=0; j<SIZE; j++)
  25.       printf("%2d ", m[i][j]);
  26.     printf("|");
  27.   }
  28. }
  29.  
  30.  
  31. void* mmult (void* slice)
  32. {
  33.   int s = (int)slice;
  34.   int from = (s * SIZE)/N;    /* note that this 'slicing' works fine */
  35.   int to = ((s+1) * SIZE)/N;    /* even if SIZE is not divisible by N */
  36.   int i,j,k;
  37.  
  38.   printf("computing slice %d (from row %d to %d)\n", s, from, to-1);
  39.   for (i=from; i<to; i++)
  40.     for (j=0; j<SIZE; j++) {
  41.       C[i][j]=0;
  42.       for (k=0; k<SIZE; k++)
  43.     C[i][j] += A[i][k]*B[k][j];
  44.     }
  45.  
  46.   printf("finished slice %d\n", s);
  47.   return 0;
  48. }
  49.  
  50. int main(int argc, char *argv[])
  51. {
  52.   pthread_t *thread;
  53.   int i;
  54.  
  55.   if (argc!=2) {
  56.     printf("Usage: %s number_of_threads\n",argv[0]);
  57.     exit(-1);
  58.   }
  59.  
  60.   N=atoi(argv[1]);
  61.   fill_matrix(A);
  62.   fill_matrix(B);
  63.   thread = malloc(N*sizeof(pthread_t));
  64.  
  65.   for (i=1; i<N; i++) {
  66.     if (pthread_create (&thread[i], NULL, mmult, (void*)i) != 0 ) {
  67.       perror("Can't create thread");
  68.       exit(-1);
  69.     }
  70.   }
  71.  
  72.   /* master thread is thread 0 so: */
  73.   mmult(0);
  74.  
  75.   for (i=1; i<N; i++) pthread_join (thread[i], NULL);
  76.  
  77.   printf("\n\n");
  78.   print_matrix(A);
  79.   printf("\n\n\t       * \n");
  80.   print_matrix(B);
  81.   printf("\n\n\t       = \n");
  82.   print_matrix(C);
  83.   printf("\n\n");
  84.  
  85.   return 0;
  86.  
  87. }
  88.  
Any help would be appreciated, thanks in advance
Sep 16 '10 #4
ashitpro
542 Recognized Expert Contributor
Explicit type casting should work..

Expand|Select|Wrap|Line Numbers
  1. thread = (pthread_t *)malloc(N*sizeof(pthread_t));
  2.  
Sep 16 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

11
2535
by: Michael Bader | last post by:
Hi, I'm currently working on a matrix multiplication code (matrix times matrix), and have come along some interesting/confusing results concerning the running time of the (apparently) same algorithm, when implemented in C or C++. I noticed that the implementation in C is faster by a factor of 2.5 compared to a identical(?) C++-implementation. The basic algorithm in C is:
7
18848
by: sandhya | last post by:
Write a program for matrix multiplication using pointers --pls.help
0
4201
by: lituncse | last post by:
dear friends, i have come across a problem which is difficult to solve for me.it's about starssen's matrix multiplication.in general matrix multiplication we need 8 multiplications and 4 additions but for the starssen's matrix multiplication we need 7 multiplication and 14 additions which is very important from time complexity point of view.please give a solution if anyone knows. ratikanta panda
6
16430
by: amitsoni.1984 | last post by:
Hi, Is there any direct function for matrix multiplication in Python or any of its packages? or do we have to multiply element by element? Thank you, Amit
3
4691
by: ABOD | last post by:
hi all...plzz..help me to solve this Q. write a multithreading program to multiply ttwo matrices M and N. The two matrices may have differnrt size. M is (r*w) and N is(w*z) The result is a r*z Matrix. You need to compute the result matrix elements concurrently.
1
9149
by: Sozos | last post by:
Hi guys. I have a problem with writing the base case for the following matrix multiplication function I have implemented. Please help. #define index(i,j,power) (((i)<<(power))+(j)) void recMultiply(int i, int j, float a, int k, int l, float b, int x, int y, float c, int s); int i, j, k, s, matrixsize, blocksize, jj, kk, power, bsize; float sum, maxr, total=0.0, startmult, finishmult, multtime; float* A = NULL; float* B = NULL;
8
7880
by: joegao1 | last post by:
can some one give me a hint? I want to program the code for matrix multiplication with as less arithmetical / multiplication operations as possible. my task is to calculate the matrix multiplication A*B*A' , where - A' refers to the transpose of matrix A - matrix B is symmetric matrix. - (optional) it will be much better if the result of (A*B) or (A*B)' can be stored as temperal matrix, as this value is required thereafter.
13
2832
mia023
by: mia023 | last post by:
hello everyone I have a project and just need ideas about it. This is the documentation: Develop the code for matrix multiplication in Peano‐Order and another one in roworder. The program should be able to read two matrices from a file, and output the multiplication of these two matrices using both orders. Use PAPI (Performance Application Programming Interface) to instrument your code. Your code should monitor the following: L1 cache...
2
6968
by: gchidam | last post by:
please help me out in matrix multiplication using pointers in C++ and give me brief description about usage of pointers
0
8825
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7327
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4152
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.