Click here to Skip to main content
15,905,148 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include <stdio.h>
#include <pthread.h>
#include <time.h>

void matrixMultiply(int a[][2000], int b[][2000], int c[][2000]);
//void oneThread(pthread_t thread[]);
//void twoThread(pthread_t thread[]);
//void fourThread(pthread_t thread[]);
//void eightThread(pthread_t thread[]);
//void sixteenThread(pthread_t thread[]);



int main(int argc, const char* argv[] ){
	srand(time(0));
	pthread_t threads[16];
	
	int matrixOne[2000][2000];
	int matrixTwo[2000][2000];
	int productMatrix[2000][2000];
	
	int i;
	int j = 0;
	for(i = 0; i < 2000; i++){
		for(j = 0; j < 2000; j++){
			matrixOne[i][j] = rand();
			//printf("value at %d, %d, is %d", i, j, matrixOne[i][j]);
		}
	}
	i = 0;
	j = 0;
	for(i = 0; i < 2000; i++){
		for(j = 0; j < 2000; j++){
			matrixTwo[i][j] = rand();
			//printf("value at %d, %d, is %d", i, j, matrixTwo[i][j]);
		}
	}
	matrixMultiply(matrixOne, matrixTwo, productMatrix);
	return 0;
}

void matrixMultiply(int matrix1[2000][2000], int matrix2[2000][2000], int pM[2000][2000]){
	
	int c,d,k,sum = 0;
	for(c = 0; c < 2000; c++){
		for(d = 0; d < 2000; d++){
			for(k = 0; k < 2000; k++){
				sum = sum + matrix1[c][k] * matrix2[k][d];
			}
			pM[c][d] = sum;
			sum = 0;
		}
	}
    }
Posted
Updated 5-Nov-15 13:17pm
v2
Comments
barneyman 5-Nov-15 19:21pm    
cursory examination looks ok - have you tried debugging it?
[no name] 5-Nov-15 19:40pm    
This type of code can be a devil to get running. You will need patience and the debugger. I suggest you learn how to use assert() http://www.programmingsimplified.com/c/source-code/assert
This will assist you to zero in on the problem.
Mohibur Rashid 5-Nov-15 19:57pm    
OS fails to allocate memory for your enormous data in stack. Almost 46MB alone. If you are in linux system, then try this:
#this value will be for current session, I guess. If your machine get screwed... not taking any responsibility
$ulimit -s
8192 --remember the value
$ulimit -s 50000000000 --I pressed down 0 button until I got tired.

and now run your executable. It will be executed.

after that you can set back
#ulimit -s 8192
barneyman 5-Nov-15 21:13pm    
good catch!

Thoroughly check up everything under the debugger. In such simple application codes, segmentation fault can be a result of addressing the array element outside of its actual domain of valid addresses. In other words, one of the values of one of your indices gets value greater that its valid value. Runtime cannot detect such situation until you eventually hit the segment boundary.
—SA
 
Share this answer
 
v2
Comments
Patrice T 5-Nov-15 21:10pm    
Didn't play with C since 15 years.
But I wonder if it is possible to call a function with 3 parameters of 4M integers each without pointers ?
Looks big to me, in segmented memory model.
[no name] 5-Nov-15 21:17pm    
"The answer is that the C compiler does a little something behind your back. It knows that whenever you mention an array name in an expression, it (the compiler) generates a pointer to the array's first element. Therefore, it knows that a function can never actually receive an array as a parameter. Therefore, whenever it sees you defining a function that seems to accept an array as a parameter, the compiler quietly pretends that you had declared it as accepting a pointer, instead. The definition of getline above is compiled exactly as if it had been written"
https://www.eskimo.com/~scs/cclass/notes/sx10f.html
Sergey Alexandrovich Kryukov 5-Nov-15 21:54pm    
Absolutely. The segmented memory model produced segment-related hardware exceptions, but the memory allocation actually uses some sub-allocator, using bigger memory chunks to place smaller objects. If, by a bug, one crosses the valid boundary between objects, the exception may or may not be thrown, if not, just the garbage in memory is created. With paged model it's more complicated, because page faults is used for implementation of virtual memory...
—SA
Patrice T 5-Nov-15 22:58pm    
Ok, silly compiler :)
I was used to declare pointers when needed :)
The code looks OK. You should read this link:
http://unix.stackexchange.com/questions/132192/running-application-ends-with-segmentation-fault[^]

You have not reported at which point the problem occurs: eg immediately.
You may have a problem with your stack. For testing only I suggest you change these lines:
C#
static int matrixOne[2000][2000];
static int matrixTwo[2000][2000];
static int productMatrix[2000][2000];

This will cause the compiler allocate the arrays in the DATA segment thus not on the stack at runtime.

The next step is the use of assert() and the debugger.

Once you have found the problem the arrays should be allocated on the heap using malloc() (and free()) and accessed via pointers.
http://web.cs.swarthmore.edu/~newhall/unixhelp/C_arrays.html[^]
 
Share this answer
 
v3
I have 2 things
- first, the function prototype
C++
void matrixMultiply(int a[][2000], int b[][2000], int c[][2000]);

and the function declaration
C++
void matrixMultiply(int matrix1[2000][2000], int matrix2[2000][2000], int pM[2000][2000]){

do not match.
May be not an error, but I always found a good habit to have both match. It have avoided a couple headache in the past :)

when you will have solved your segment problem, you will run into another one.
the sum of 2000 products of random integers will never fit into an integer.
The problem is so huge that you have little chances to get a single sum correct over the 4000000 sums.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900