• 2019 Update 7
  • 03/31/2020
Contents

Program Examples

Intel® MPI Library Developer Guide for Linux* OS

pthreads - Explicit Submodel

#include <mpi.h> #include <pthread.h> #define n 2 int thread_id[n]; MPI_Comm split_comm[n]; pthread_t thread[n]; void *worker(void *arg) { int i = *((int*)arg), j = i; MPI_Comm comm = split_comm[i]; MPI_Allreduce(MPI_IN_PLACE, &j, 1, MPI_INT, MPI_SUM, comm); printf("Thread %d: allreduce returned %d\n", i, j); } int main() { MPI_Info info; int i, provided; char s[16]; MPI_Init_thread(NULL, NULL, MPI_THREAD_MULTIPLE, &provided); MPI_Info_create(&info); for (i = 0; i < n; i++) { MPI_Comm_dup(MPI_COMM_WORLD, &split_comm[i]); sprintf(s, "%d", i); MPI_Info_set(info, "thread_id", s); MPI_Comm_set_info(split_comm[i], info); thread_id[i] = i; pthread_create(&thread[i], NULL, worker, (void*) &thread_id[i]); } for (i = 0; i < n; i++) { pthread_join(thread[i], NULL); } MPI_Info_free(&info); MPI_Finalize(); }

OpenMP Runtime - Implicit Submodel

#include <mpi.h> #include <omp.h> #define n 2 MPI_Comm split_comm[n]; int main() { int i, provided; MPI_Init_thread(NULL, NULL, MPI_THREAD_MULTIPLE, &provided); for (i = 0; i < n; i++) MPI_Comm_dup(MPI_COMM_WORLD, &split_comm[i]); #pragma omp parallel for num_threads(n) for (i = 0; i < n; i++) { int j = i; MPI_Allreduce(MPI_IN_PLACE, &j, 1, MPI_INT, MPI_SUM, split_comm[i]); printf("Thread %d: allreduce returned %d\n", i, j); } MPI_Finalize(); }

OpenMP Tasks - Explicit Submodel

#include <mpi.h> #include <omp.h> #define n 2 MPI_Comm split_comm[n]; int main() { MPI_Info info; int i, provided; char s[16]; MPI_Init_thread(NULL, NULL, MPI_THREAD_MULTIPLE, &provided); MPI_Info_create(&info); for (i = 0; i < n; i++) { MPI_Comm_dup(MPI_COMM_WORLD, &split_comm[i]); sprintf(s, "%d", i); MPI_Info_set(info, "thread_id", s); MPI_Comm_set_info(split_comm[i], info); } #pragma omp parallel num_threads(n) { #pragma omp task { int j = 1; MPI_Allreduce(MPI_IN_PLACE, &j, 1, MPI_INT, MPI_SUM, split_comm[1]); printf("OMP thread %d, logical thread %d: allreduce returned %d\n", omp_get_thread_num(), 1, j); } #pragma omp task { int j = 0; MPI_Allreduce(MPI_IN_PLACE, &j, 1, MPI_INT, MPI_SUM, split_comm[0]); printf("OMP thread %d, logical thread %d: allreduce returned %d\n", omp_get_thread_num(), 0, j); } } MPI_Info_free(&info); MPI_Finalize(); }

Product and Performance Information

1

Intel's compilers may or may not optimize to the same degree for non-Intel microprocessors for optimizations that are not unique to Intel microprocessors. These optimizations include SSE2, SSE3, and SSSE3 instruction sets and other optimizations. Intel does not guarantee the availability, functionality, or effectiveness of any optimization on microprocessors not manufactured by Intel. Microprocessor-dependent optimizations in this product are intended for use with Intel microprocessors. Certain optimizations not specific to Intel microarchitecture are reserved for Intel microprocessors. Please refer to the applicable product User and Reference Guides for more information regarding the specific instruction sets covered by this notice.

Notice revision #20110804