1

When I test the following code with #define N 100 , there's no error accures. But I change it into #define N 1000, error show's that fish: “./file” terminated by signal SIGSEGV (Address boundary error). Can somebody help? I'v tried to find answers from google, but I can't understand other's solutions which since to be same error different problem.... I know that multiply two matrix can work in a large amount such as 1000*1000 matrix. But I'm new to learn this multiply matrix.

#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#include <time.h>
#define N 2000

int main(){
   struct timespec t_start, t_end;
   double elapsedTime;
   int i, j, k;
   int a[N][N], b[N][N], c[N][N], cc[N][N];
   
   for( i=0; i<N; i++ )
    for( j=0; j<N; j++ ) {
        a[i][j] = rand();
        b[i][j] = rand();
        c[i][j] = 0;
        cc[i][j] = 0;
    }
    // start time
    clock_gettime( CLOCK_REALTIME, &t_start);  
    for( i=0; i<N; i++ ){
        for( j=0; j<N; j++ ) {
            for( k=0; k<N; k++ ){
                c[i][j] += a[i][k]*b[k][j];
            }
        }
    }
    // stop time
   clock_gettime( CLOCK_REALTIME, &t_end);

   // compute and print the elapsed time in millisec
   elapsedTime = (t_end.tv_sec - t_start.tv_sec) * 1000.0;
   elapsedTime += (t_end.tv_nsec - t_start.tv_nsec) / 1000000.0;
   printf("Sequential elapsedTime: %lf ms\n", elapsedTime);  
    // start time
   clock_gettime( CLOCK_REALTIME, &t_start);  
   #pragma omp parallel for private(i, j, k)  
   for( i=0; i<N; i++ )
    for( j=0; j<N; j++ ) {
        cc[i][j] = 0;
        for( k=0; k<N; k++ )
            cc[i][j] += a[i][k]*b[k][j];
    }
 
  // stop time
   clock_gettime( CLOCK_REALTIME, &t_end);

   // compute and print the elapsed time in millisec
   elapsedTime = (t_end.tv_sec - t_start.tv_sec) * 1000.0;
   elapsedTime += (t_end.tv_nsec - t_start.tv_nsec) / 1000000.0;
   printf("Parallel elapsedTime: %lf ms\n", elapsedTime);
 
   for(i=0; i<N; i++){
        for(j=0; j<N; j++){
            if(cc[i][j] != c[i][j])
                break;
        }
    }
   
    if(i==N && j==N)
        printf("Test pass!!!\n"); 
    else
        printf("Test failed!!!\n");
   return 0;
}



Tried to google SIGSEGV but couldn't found what's wrong?

Julia
  • 23
  • 1
  • 6
  • `int a[N][N], b[N][N], c[N][N], cc[N][N];` When `N` is set to 1000 each of those arrays will be about 4MB in size. That is too large to be stored on the stack (which is where local variables are stored). Use dynamic allocation instead. See for example: [Correctly allocating multi-dimensional arrays](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays) – kaylum Mar 16 '21 at 06:05
  • You are having stack overflow, most system has default max stack size of 8MiB. You should allocate large amount of memory on the heap, see `man 3 malloc`. If you want to change the limit of stack size, you can use `ulimit -s unlimited` (that's a bad idea anyway). – Ammar Faizi Mar 16 '21 at 06:06

1 Answers1

0

SIGSEGV means SIGnal SEGmentation Violation, meaning that the code accesses memory areas that it is not allowed to access, in this case it smashes the stack ('Address boundary error'), see Nate Eldredge's and kaylum's comments under the question. The program is terminated by this signal (SIGSEGV) from the operating system. When using dynamic allocation the memory for the arrays is allocated on the heap (What and where are the stack and heap?) and so you will not have this issue (malloc(), calloc(),... : https://www.programiz.com/c-programming/c-dynamic-memory-allocation )

ralf htp
  • 9,149
  • 4
  • 22
  • 34