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. -
#include <iostream>
-
#include <stdio.h>
-
#include <pthread.h>
-
-
using namespace std;
-
-
-
const int DIM = 720; //works up to 719, crashes at 720
-
const int num_of_thr = 4;
-
int matrix_A[DIM][DIM];
-
int matrix_B[DIM][DIM];
-
int c[DIM][DIM];
-
-
struct v
-
{
-
int i;
-
int j;
-
};
-
-
//worker thread
-
void* matrix_multi(void* data)
-
{
-
for(int i = 0; i < DIM; i++)
-
{
-
for(int j = 0; j < DIM; j++)
-
{
-
c[i][j] = 0;
-
for(int k = 0; k < DIM; k++)
-
{
-
c[i][j] += matrix_A[i][k] * matrix_B[k][j];
-
}
-
}
-
}
-
pthread_exit(0);
-
}
-
-
int main()
-
{
-
-
pthread_t thr_id[DIM][DIM];
-
pthread_attr_t thr_attr;
-
pthread_attr_init(&thr_attr);
-
-
-
-
//Filling the Matrices
-
for(int i = 0; i < DIM; i++)
-
{
-
for(int j = 0; j < DIM; j++)
-
{
-
matrix_A[i][j]= i + j;
-
matrix_B[i][j] = i + 3;
-
}
-
}
-
-
-
//create the threads
-
for(int i = 0; i < num_of_thr/2; i++)
-
{
-
for(int j = 0; j < num_of_thr/2; j++)
-
{
-
struct v *data = (struct v *) malloc(sizeof(struct v));
-
data->i = i;
-
data->j = j;
-
pthread_create(&thr_id[i][j],NULL,matrix_multi, &data);
-
}
-
}
-
-
//joining the threads
-
for(int i = 0; i < num_of_thr/2; i++)
-
{
-
for(int j = 0; j < num_of_thr/2; j++)
-
{
-
pthread_join(thr_id[i][j],NULL);
-
}
-
}
-
-
return 0;
-
}
-
-
-
Any help would be appreciated, thanks in advance.
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.
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?
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: -
-
#include <pthread.h>
-
#include <stdlib.h>
-
#include <stdio.h>
-
-
#define SIZE 10 /* Size of matrices */
-
int N; /* number of threads */
-
-
int A[SIZE][SIZE], B[SIZE][SIZE], C[SIZE][SIZE];
-
-
void fill_matrix(int m[SIZE][SIZE])
-
{
-
int i, j, n = 0;
-
for (i=0; i<SIZE; i++)
-
for (j=0; j<SIZE; j++)
-
m[i][j] = n++;
-
}
-
-
void print_matrix(int m[SIZE][SIZE])
-
{
-
int i, j = 0;
-
for (i=0; i<SIZE; i++) {
-
printf("\n\t| ");
-
for (j=0; j<SIZE; j++)
-
printf("%2d ", m[i][j]);
-
printf("|");
-
}
-
}
-
-
-
void* mmult (void* slice)
-
{
-
int s = (int)slice;
-
int from = (s * SIZE)/N; /* note that this 'slicing' works fine */
-
int to = ((s+1) * SIZE)/N; /* even if SIZE is not divisible by N */
-
int i,j,k;
-
-
printf("computing slice %d (from row %d to %d)\n", s, from, to-1);
-
for (i=from; i<to; i++)
-
for (j=0; j<SIZE; j++) {
-
C[i][j]=0;
-
for (k=0; k<SIZE; k++)
-
C[i][j] += A[i][k]*B[k][j];
-
}
-
-
printf("finished slice %d\n", s);
-
return 0;
-
}
-
-
int main(int argc, char *argv[])
-
{
-
pthread_t *thread;
-
int i;
-
-
if (argc!=2) {
-
printf("Usage: %s number_of_threads\n",argv[0]);
-
exit(-1);
-
}
-
-
N=atoi(argv[1]);
-
fill_matrix(A);
-
fill_matrix(B);
-
thread = malloc(N*sizeof(pthread_t));
-
-
for (i=1; i<N; i++) {
-
if (pthread_create (&thread[i], NULL, mmult, (void*)i) != 0 ) {
-
perror("Can't create thread");
-
exit(-1);
-
}
-
}
-
-
/* master thread is thread 0 so: */
-
mmult(0);
-
-
for (i=1; i<N; i++) pthread_join (thread[i], NULL);
-
-
printf("\n\n");
-
print_matrix(A);
-
printf("\n\n\t * \n");
-
print_matrix(B);
-
printf("\n\n\t = \n");
-
print_matrix(C);
-
printf("\n\n");
-
-
return 0;
-
-
}
-
Any help would be appreciated, thanks in advance
ashitpro 542
Recognized Expert Contributor
Explicit type casting should work.. -
thread = (pthread_t *)malloc(N*sizeof(pthread_t));
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
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:
|
by: sandhya |
last post by:
Write a program for matrix multiplication using pointers --pls.help
|
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
|
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
|
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.
| |
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;
|
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.
|
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...
|
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
|
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...
|
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...
| |
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...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |