Skip to content

Instantly share code, notes, and snippets.

@tobbe11
Last active June 21, 2016 06:39
Show Gist options
  • Select an option

  • Save tobbe11/87989ed3a5d8d3a5382778a55ef2c2a4 to your computer and use it in GitHub Desktop.

Select an option

Save tobbe11/87989ed3a5d8d3a5382778a55ef2c2a4 to your computer and use it in GitHub Desktop.
CMakeLists examples
cmake_minimum_required(VERSION 2.8.9)
project (openmp)
if (CMAKE_VERSION VERSION_LESS "3.1")
set (CMAKE_C_FLAGS "--std=c11 ${CMAKE_C_FLAGS}")
set (CMAKE_CXX_FLAGS "--std=c++11 ${CMAKE_CXX_FLAGS}")
else ()
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 11)
endif ()
#include_directories(include)
#file(GLOB SOURCES "src/*.c")
set(SOURCES openmp.c)
include(FindOpenMP)
if(OPENMP_FOUND)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
endif()
add_executable(openmp ${SOURCES})
#define _GNU_SOURCE //enable asprintf
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
int wc(char *docname){
char *cmd;
asprintf(&cmd, "cat %s | wc", docname);
FILE *wc = popen(cmd, "r");
char out[100];
fread(out, 1, 100, wc);
free( cmd );
pclose( wc );
return atoi(out);
}
int main(int argc, char **argv){
int nthreads, tid;
argc--;
argv++;
if(!argc){
fprintf(stderr, "Enter some file names.");
return 0;
}
long int total_wc=0;
#pragma omp parallel for reduction(+:total_wc)
for (int i=0; i< argc; i++){
int ct = wc(argv[i]);
printf("%s:\t%i\n", argv[i], ct);
total_wc += ct;
} /* All threads join master thread and disband */
printf("Σ:\t%li\n", total_wc);
/* Fork a team of threads giving them their own copies of variables */
#pragma omp parallel private(nthreads, tid)
{
tid = omp_get_thread_num(); /* Obtain thread number */
printf("Hello World from thread = %d\n", tid);
#pragma omp barrier
if (tid == 0) /* Only master thread does this */
{
nthreads = omp_get_num_threads();
printf("Number of threads = %d\n", nthreads);
}
} /* All threads join master thread and disband */
}
// Compile with 'gcc openmp.c -std=c11 -fopenmp -o openmp'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment