Clion下简单的并行计算 Keywords: OpenMP Cmake Clion C语言 Linux

Project名为Tests,目的为实现4核并行输出hello。

CMakeList.txt的内容如下:

cmake_minimum_required(VERSION 3.9)
project(Tests C)

set(CMAKE_C_STANDARD 99)

FIND_PACKAGE( OpenMP REQUIRED)
if(OPENMP_FOUND)
    message("OPENMP FOUND")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
endif()

set(SOURCE_FILES main.c)
add_executable(Tests ${SOURCE_FILES})

main.c的内容如下:

#include 
#include 

void hello()
{
    int my_rank=omp_get_thread_num();
    int thread_count=omp_get_num_threads();

    printf("hello from rank %d of %d \n",my_rank,thread_count);
}

int main(int argc, char* argv) {

    int thread_count=4;
    #pragma omp parallel num_threads(thread_count)
    hello();

    return 0;
}

输出结果为:

hello from rank 0 of 4 
hello from rank 1 of 4 
hello from rank 3 of 4 
hello from rank 2 of 4 

你可能感兴趣的:(Parallel,Computing)