Fedora Linux Support Community & Resources Center
  #1  
Old 10th May 2012, 05:20 AM
aitm04 Offline
Registered User
 
Join Date: May 2012
Location: belgaum
Posts: 2
linuxchrome
Parallel programming

Thanks for the suggestion....Here is the code which is giving segmentation fault error(core dump)

Source Code

#include<stdio.h>
#include<omp.h>
int main()
{
int A[1000000],B[1000000],C[1000000];
int I;
double start,end,seq,par;
for(I=1;I<=1000000;I++)
{
A[I]=I;
B[I]=I;
}
start=omp_get_wtime();
for(I=1;I<=1000000;I++)
C[I]=A[I]+B[I];
end=omp_get_wtime();
seq=(end-start);
start=omp_get_wtime();
#pragma omp parallel for
for(I=1;I<=1000000;I++)
C[I]=A[I]+B[I];
end=omp_get_wtime();
par=(end-start);
printf("sequential time execution is=%lf",seq);
printf("parallel time execution is=%lf",par);
printf("\n the elements of array A are:\n");
for(I=1;I<=20;I++)
printf("%5d",A[I]);
printf("_ _ _ _ _ _ 1000000\n");
printf("\n the elements of array B are:\n");
for(I=1;I<=20;I++)
printf("%5d",B[I]);
printf("_ _ _ _ _ _ 1000000\n");
printf("\n the elements of array C are:\n");
for(I=1;I<=20;I++)
printf("%5d",C[I]);
printf("\n time taken for sequential execution=%lf secs",seq);
printf("\n time taken for parallel execution=%lf secs",par);
} 1,1 Top
Reply With Quote
  #2  
Old 10th May 2012, 06:08 AM
marko Offline
Registered User
 
Join Date: Jun 2004
Location: Laurel, MD USA
Posts: 6,062
linuxfirefox
Re: Parallel programming

It would help a lot if you would wrap the code in a code block so the indention is correctly visible (hit the "go advanced" button, highlight the code and use the "#" code block wrapper).

The problem seems to be the indexing:
Code:
int A[1000000],B[1000000],C[1000000];
....
for(I=1;I<=1000000;I++)
C[I]=A[I]+B[I];
you are indexing I with 1 to N style, where N is 1,000,000 due to your for test that used "<=" which is wrong. This will let 'I' reach 1000000 which will be one off the end of the arrays. Try this instead which will range I from 0 to 999,999

for( I = 0; I < 1000000; I++)

It would be a a bit clearer to use a local const for the limit to both size the arrays and set the for loop limit:
Code:
const int SIZE = 1000000;
int A[SIZE], B[SIZE], C[SIZE];
....
for(I=0; I < SIZE; I++)
......
That way it's obvious how the sizes and the loop limits are related (and easier to change)

It's also possible that 12 megabytes (3 arrays of 1000000 32bit ints) is too much stack usage for the function stack size.
I'm not sure how but I think you can get g++ to show the current stack size for your machine, but I think if you compile your
code with -fstack-check option, it will at least test that your stack usage doesn't go too large and tell you if it does.

REF: http://gcc.gnu.org/onlinedocs/gccint...-Checking.html

I did a little test and found that I could run a cpp exe with a function containing two 1,000,000 int arrays but not with three (it segfaults)

Last edited by marko; 10th May 2012 at 06:21 AM.
Reply With Quote
  #3  
Old 10th May 2012, 09:06 PM
stevea Online
Registered User
 
Join Date: Apr 2006
Location: Ohio, USA
Posts: 8,917
linuxfirefox
Re: Parallel programming

Here is a more idiomatic flavor of your code
Code:
#include <stdio.h>
#include <omp.h>

int main()
{
  int const SIZE=1000000, SAMPLE = 20;
  int i, A[SIZE], B[SIZE], C[SIZE];
  double start, end, seq, par;

  for(i = 0; i < SIZE; i++) {
      A[i] = i;
      B[i] = i;
  }

  // sequential test
  start = omp_get_wtime();
  for(i = 0; i < SIZE; i++)
    C[i] = A[i] + B[i];
  end = omp_get_wtime();
  seq = (end - start);

  // parallel test
  start = omp_get_wtime();
#pragma omp parallel for
    for(i = 0; i < SIZE; i++) {
    C[i] = A[i] + B[i];
    }
  end = omp_get_wtime();
  par = (end - start);

  printf("sequential time execution is = %lf\n", seq);
  printf("parallel   time execution is = %lf\n", par);

  printf(" the elements of array A are:\n");
  for(i = 0; i < SAMPLE; i++)
    printf("%5d",A[i]);
  printf(" ... %5d\n\n", A[SIZE-1]);

  printf(" the elements of array B are:\n");
  for(i = 0; i < SAMPLE; i++)
    printf("%5d", B[i]);
  printf(" ... %5d\n\n", B[SIZE-1]);

  printf(" the elements of array C are:\n");
  for(i = 0; i < 20; i++)
    printf("%5d", C[i]);
  printf(" ... %5d\n\n", C[SIZE-1]);

  printf(" time taken for sequential execution = %lf secs\n", seq);
  printf(" time taken for parallel   execution = %lf secs\n", par);
}

You need to compile it like ....
gcc -fopenmp -o foo foo.c

Then there is the issue of the crash ...
Quote:
[stevea@hypoxylon tmp]$ ./foo
Segmentation fault (core dumped)

Each of the 3 arrrays uses 4MB of stack space. That's not huge by modern standards, BUT the default ulimit is probably too small

Code:
[stevea@hypoxylon tmp]$ ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 61815
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 1024
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
So obviously 8MB of stack won't do the job, we need to set that limit higher.
Code:
[stevea@hypoxylon tmp]$ ulimit -s unlimited  # remove stack limit
[stevea@hypoxylon tmp]$ ./foo
sequential time execution is = 0.007456
parallel   time execution is = 0.011880
 the elements of array A are:
    0    1    2    3    4    5    6    7    8    9   10   11   12   13   14   15   16   17   18   19 ... 999999

 the elements of array B are:
    0    1    2    3    4    5    6    7    8    9   10   11   12   13   14   15   16   17   18   19 ... 999999

 the elements of array C are:
    0    2    4    6    8   10   12   14   16   18   20   22   24   26   28   30   32   34   36   38 ... 1999998

 time taken for sequential execution = 0.007456 secs
 time taken for parallel   execution = 0.011880 secs



[stevea@hypoxylon tmp]$ OMP_NUM_THREADS=50 ./foo
sequential time execution is = 0.007107
parallel   time execution is = 0.005135
 the elements of array A are:
    0    1    2    3    4    5    6    7    8    9   10   11   12   13   14   15   16   17   18   19 ... 999999

 the elements of array B are:
    0    1    2    3    4    5    6    7    8    9   10   11   12   13   14   15   16   17   18   19 ... 999999

 the elements of array C are:
    0    2    4    6    8   10   12   14   16   18   20   22   24   26   28   30   32   34   36   38 ... 1999998

 time taken for sequential execution = 0.007107 secs
 time taken for parallel   execution = 0.005135 secs
You may need
ulimit -u 40000
to try really large numbers of threads.

---------- Post added at 04:02 PM ---------- Previous post was at 03:58 PM ----------

PS - you really need more calculation in the loop to take good advantage of parallelism. Otherwise the thread overhead costs more than it saves.

---------- Post added at 04:06 PM ---------- Previous post was at 04:02 PM ----------

P.P.S> Is it true they charge programmers 10 euro-cents for each extra space and a full euro for extra newlines in Belgium ?
__________________
None are more hopelessly enslaved than those who falsely believe they are free.
Johann Wolfgang von Goethe

Last edited by stevea; 10th May 2012 at 09:00 PM.
Reply With Quote
Reply

Tags
parallel, programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Parallel programming aitm04 Using Fedora 3 10th May 2012 02:26 PM
Parallel Processing? Ernst0 Programming & Packaging 3 5th February 2012 02:52 PM
linux programming vs. windows programming unlovedwarrior Programming & Packaging 11 22nd October 2006 09:14 PM
Parallel port programming in c/c++ JPEM Programming & Packaging 0 10th October 2005 11:53 AM


Current GMT-time: 09:32 (Friday, 24-10-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat
Las Cruces - Branford Photos - Istra Travel Photos