/*
    Copyright 2003-2019 Intel Corporation.
    
    This software and the related documents are Intel copyrighted materials, and
    your use of them is governed by the express license under which they were
    provided to you (License). Unless the License provides otherwise, you may
    not use, modify, copy, publish, distribute, disclose or transmit this
    software or the related documents without Intel's prior written permission.
    
    This software and the related documents are provided as is, with no express
    or implied warranties, other than those that are expressly stated in the
    License.
*/
#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();
}
