Compile a C program with GCC, so that it can use all cpu cores in linux

后端 未结 4 1009
孤独总比滥情好
孤独总比滥情好 2020-12-16 08:35

I have a sample C program for addition. When I compile and run it with GCC it is using only one CPU core.

Is there any way to compile a C program so that it can use

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-16 09:28

    Try adding the following pragma right above your for loops:

    #pragma omp parallel for
    for(i=0; i<8000000000; i++) {
        ptr[i] = i/10000;
    }
    

    And add the -fopenmp option to your build options when you call gcc. By default, OpenMP will create as many threads as cores in your machine and will share workload evenly between them.

    You can check this article for more information on OpenMP.

提交回复
热议问题