Page 1 of 1

Basic gcc question

PostPosted: Apr 29th, '14, 09:41
by ryk
Hi again,

because i don't not know almost anything about programming, may i ask a -silly to many others- question.
I have to compile a c program (nq.c) with gcc . I have used same program on win. How can I compile it, so when i run it, to use both (or as many) cores of my cpu?
I tried man gcc, but there is much information and it is difficult for me to understand. Also, I played with -march options, but only x86_64 worked. The type of the OS is important? (meaning, i can't make a x86_32 executable on a 64 bit system?)

Re: Basic gcc question

PostPosted: Apr 29th, '14, 14:56
by wintpe
while there are options in the compiler to change the optimisations surrounding thread aware code.

the way you write a program determines if it is multi threaded.

for example, outside of programming C code, you could split up a job processing lets say 100 mpeg2 files into mpeg4 by splitting the job up, into 4 x 25 files
and running 4 single threaded conversion tools in parallel.

this is now a multithreaded job.

when you write a C program you have to split the work up into multiple threads so that it can take advantage of multiple CPU cores, or sockets.

a multithreaded mpeg2 to mpeg4 conversion program, may spit the video file, and the audio file up and break the video into chunks and then spawn multiple threads in parallel, to deal with each chunk, and then finally re-assemble the chunks into one.

this is why you sometimes hear that multicore processors such as the bulldozer and its derivatives, dont perform well in single threaded games.

there's no magical way to make that game suddenly perform faster and use all cores without a rewrite.

to compile your program, (and im assuming its simple, and no need for libraries or includes)

gcc nc.c -o nc

if it needs libraries or includes, then

gcc nc.c -I/usr/include -L/usr/lib -o nc

you will know if it needs libraries etc as the first simple compile will complain about missing include or missing libabry, or unknown function etc.

regards peter

Re: Basic gcc question

PostPosted: May 5th, '14, 13:08
by ryk
Thank you Peter for your enlightening answer! The program is a solution to n queen problem. It is not mine, and the programmer had the kindness to give me the source code, but i don't know much about programming....
Perhaps you'd like to try! ;)

Re: Basic gcc question

PostPosted: May 5th, '14, 17:07
by tom_
try this commands sequence (assuming you have gcc installed):

Code: Select all
# rename the source file  in .c
mv nq.txt nq.c

# compile it
gcc nq.c -lpthread -o aNameYouPrefer

# use you executable
./aNameYouPrefer  s 2 3

Re: Basic gcc question

PostPosted: May 6th, '14, 01:19
by ryk
I tried it, but the program recognize only one option (the size of the chessboard). The "s 2 3" does not work.

Re: Basic gcc question

PostPosted: May 6th, '14, 01:44
by tom_
Code: Select all
int nqueen(char * strategy, int n, int parallel_depth) {
  if (strategy[0] == 's') {
    printf("serial\n");
    return nqs(0, 0, 0, 0, n);
  } else if (strategy[0] == 'n') {
    printf("naive\n");
    return nqp(0, 0, 0, 0, n, parallel_depth);
  } else if (strategy[0] == 'o') {
    printf("omp\n");
    return nqw_omp_master(n, parallel_depth);
  } else if (strategy[0] == 'p') {
    printf("worker by pthread\n");
    return nqw_pth_master(n, parallel_depth);
  } else if (strategy[0] == 'w') {
    printf("worker by pthread2\n");
    return nqw2_pth_master(n, parallel_depth);
  } else {
    printf("no such strategy %s\n", strategy);
    exit(1);
  }
}


int main(int argc, char ** argv) {
  char * strategy = argv[1];
  int n = atoi(argv[2]);
  int p = -1;
  if (argc > 3) p = atoi(argv[3]);
  double t0 = cur_time();
  int count = nqueen(strategy, n, p);
  double t1 = cur_time();
  printf("%.3f sec (%d queen = %d)\n", t1 - t0, n, count);
}


as you can see it accepts as first parameter one of the letters s n o p w,
then a couple of numbers; but I have no idea of what this task does :)

Re: Basic gcc question

PostPosted: May 6th, '14, 08:28
by ryk
Hi Tom, it's a solution to n queens problem: http://en.wikipedia.org/wiki/Eight_queens_puzzle
But in usage it accepts only an integer (the size of the table) e.g.: ./nq 16 (calculates the results for a 16x16 table). If you have the time, try it. I use it to see how fast is the cpu after overclocking, to stress it in undervoltage situation etc.

Re: Basic gcc question

PostPosted: May 7th, '14, 00:33
by jiml8
Well, without looking any deeper into it, I can tell you that as written it won't work.

The variable strategy[0] always contains the name of the program (which is the first argument in the command line). I think that the desired variable to test here is strategy[1].