Basic gcc question

This forum is dedicated to basic help and support :

Ask here your questions about basic installation and usage of Mageia. For example you may post here all your questions about getting Mageia isos and installing it, configuring your printer, using your word processor etc.

Try to ask your questions in the right sub-forum with as much details as you can gather. the more precise the question will be, the more likely you are to get a useful answer

Basic gcc question

Postby ryk » Apr 29th, '14, 09:41

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?)
ryk
 
Posts: 30
Joined: Oct 13th, '11, 10:02

Re: Basic gcc question

Postby wintpe » Apr 29th, '14, 14:56

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
Redhat 6 Certified Engineer (RHCE)
Sometimes my posts will sound short, or snappy, however its realy not my intention to offend, so accept my apologies in advance.
wintpe
 
Posts: 1204
Joined: May 22nd, '11, 17:08
Location: Rayleigh,, Essex , UK

Re: Basic gcc question

Postby ryk » May 5th, '14, 13:08

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! ;)
Attachments
nq.txt
(11.15 KiB) Downloaded 94 times
ryk
 
Posts: 30
Joined: Oct 13th, '11, 10:02

Re: Basic gcc question

Postby tom_ » May 5th, '14, 17:07

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
tom_
 
Posts: 423
Joined: Sep 3rd, '11, 12:26
Location: Porto Ercole, Italy

Re: Basic gcc question

Postby ryk » May 6th, '14, 01:19

I tried it, but the program recognize only one option (the size of the chessboard). The "s 2 3" does not work.
ryk
 
Posts: 30
Joined: Oct 13th, '11, 10:02

Re: Basic gcc question

Postby tom_ » May 6th, '14, 01:44

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 :)
tom_
 
Posts: 423
Joined: Sep 3rd, '11, 12:26
Location: Porto Ercole, Italy

Re: Basic gcc question

Postby ryk » May 6th, '14, 08:28

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.
ryk
 
Posts: 30
Joined: Oct 13th, '11, 10:02

Re: Basic gcc question

Postby jiml8 » May 7th, '14, 00:33

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].
jiml8
 
Posts: 1254
Joined: Jul 7th, '13, 18:09


Return to Basic support

Who is online

Users browsing this forum: No registered users and 1 guest

cron