Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Do connect all log channel manually to parent using XBT_LOG_CONNECT() too, so that...
[simgrid.git] / src / smpi / smpi_base.c
index bb266ea..c119790 100644 (file)
@@ -1,49 +1,38 @@
 #include "private.h"
 
 #include "private.h"
 
+XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_base, smpi, "Logging specific to SMPI (base)");
+
 smpi_mpi_global_t smpi_mpi_global = NULL;
 
 smpi_mpi_global_t smpi_mpi_global = NULL;
 
-void smpi_mpi_land_func(void *x, void *y, void *z);
+void smpi_mpi_land_func(void *a, void *b, int *length, MPI_Datatype *datatype);
 
 
-void smpi_mpi_land_func(void *x, void *y, void *z)
+void smpi_mpi_land_func(void *a, void *b, int *length, MPI_Datatype *datatype)
 {
 {
-       *(int *)z = *(int *)x && *(int *)y;
+       int i;
+       if (*datatype == smpi_mpi_global->mpi_int) {
+               int *x = a, *y = b;
+               for (i = 0; i < *length; i++) {
+                       y[i] = x[i] && y[i];
+               }
+       }
 }
 
 }
 
-void smpi_mpi_sum_func(void *x, void *y, void *z);
+void smpi_mpi_sum_func(void *a, void *b, int *length, MPI_Datatype *datatype);
 
 
-void smpi_mpi_sum_func(void *x, void *y, void *z)
+void smpi_mpi_sum_func(void *a, void *b, int *length, MPI_Datatype *datatype)
 {
 {
-       *(int *)z = *(int *)x + *(int *)y;
-}
-
-int inline smpi_mpi_comm_size(smpi_mpi_communicator_t comm, int *size)
-{
-       int retval = MPI_SUCCESS;
-       if (NULL == size) {
-               retval = MPI_ERR_ARG;
-       } else {
-               *size = comm->size;
+       int i;
+       if (*datatype == smpi_mpi_global->mpi_int) {
+               int *x = a, *y = b;
+               for (i = 0; i < *length; i++) {
+                       y[i] = x[i] + y[i];
+               }
        }
        }
-       return retval;
 }
 
 }
 
-// FIXME: smarter algorithm?
-int smpi_mpi_comm_rank(smpi_mpi_communicator_t comm, int *rank)
+int smpi_mpi_comm_rank(smpi_mpi_communicator_t comm)
 {
 {
-       int i;
-       int retval = MPI_SUCCESS;
-
-       if (NULL == rank) {
-               retval = MPI_ERR_ARG;
-       } else {
-               smx_host_t host = SIMIX_host_self();
-
-               for (i = 0; i < comm->size && host != smpi_global->hosts[comm->rank_to_index_map[i]]; i++);
-
-               *rank = i;
-       }
-
-       return retval;
+       return comm->index_to_rank_map[smpi_host_index()];
 }
 
 void smpi_mpi_init()
 }
 
 void smpi_mpi_init()
@@ -52,7 +41,18 @@ void smpi_mpi_init()
        smx_host_t *hosts;
        int host_count;
        int i;
        smx_host_t *hosts;
        int host_count;
        int i;
-
+       smpi_host_data_t hdata;
+
+   /* Connect our log channels: that must be done manually under windows */
+   /* (should be done only once, not for each process) */
+   XBT_LOG_CONNECT(smpi_base, smpi);
+   XBT_LOG_CONNECT(smpi_bench, smpi);
+   XBT_LOG_CONNECT(smpi_kernel, smpi);
+   XBT_LOG_CONNECT(smpi_mpi, smpi);
+   XBT_LOG_CONNECT(smpi_receiver, smpi);
+   XBT_LOG_CONNECT(smpi_sender, smpi);
+   XBT_LOG_CONNECT(smpi_util, smpi);
+   
        SIMIX_mutex_lock(smpi_global->running_hosts_count_mutex);
        smpi_global->running_hosts_count++;
        SIMIX_mutex_unlock(smpi_global->running_hosts_count_mutex);
        SIMIX_mutex_lock(smpi_global->running_hosts_count_mutex);
        smpi_global->running_hosts_count++;
        SIMIX_mutex_unlock(smpi_global->running_hosts_count_mutex);
@@ -62,8 +62,18 @@ void smpi_mpi_init()
        hosts      = SIMIX_host_get_table();
        host_count = SIMIX_host_get_number();
 
        hosts      = SIMIX_host_get_table();
        host_count = SIMIX_host_get_number();
 
+       hdata      = xbt_new(s_smpi_host_data_t, 1);
+
+       for (i = 0; i < host_count && host != hosts[i]; i ++);
+
+       hdata->index = i;
+       hdata->mutex = SIMIX_mutex_init();
+       hdata->cond  = SIMIX_cond_init();
+
+       SIMIX_host_set_data(host, hdata);
+
        // node 0 sets the globals
        // node 0 sets the globals
-       if (host == hosts[0]) {
+       if (0 == i) {
 
                smpi_global->hosts                              = hosts;
                smpi_global->host_count                         = host_count;
 
                smpi_global->hosts                              = hosts;
                smpi_global->host_count                         = host_count;
@@ -107,7 +117,7 @@ void smpi_mpi_init()
 
                // make sure root is done before own initialization
                SIMIX_mutex_lock(smpi_global->start_stop_mutex);
 
                // make sure root is done before own initialization
                SIMIX_mutex_lock(smpi_global->start_stop_mutex);
-               if (!smpi_global->root_ready) {
+               while (!smpi_global->root_ready) {
                        SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
                }
                SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
                        SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
                }
                SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
@@ -117,11 +127,12 @@ void smpi_mpi_init()
        // wait for all nodes to signal initializatin complete
        SIMIX_mutex_lock(smpi_global->start_stop_mutex);
        smpi_global->ready_process_count++;
        // wait for all nodes to signal initializatin complete
        SIMIX_mutex_lock(smpi_global->start_stop_mutex);
        smpi_global->ready_process_count++;
-       if (smpi_global->ready_process_count < 3 * host_count) {
-               SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
-       } else {
+       if (smpi_global->ready_process_count >= 3 * host_count) {
                SIMIX_cond_broadcast(smpi_global->start_stop_cond);
        }
                SIMIX_cond_broadcast(smpi_global->start_stop_cond);
        }
+       while (smpi_global->ready_process_count < 3 * host_count) {
+               SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
+       }
        SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
 
        return;
        SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
 
        return;
@@ -139,6 +150,9 @@ void smpi_mpi_finalize()
        smpi_global->ready_process_count--;
        SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
 
        smpi_global->ready_process_count--;
        SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
 
+       SIMIX_mutex_destroy(smpi_host_mutex());
+       SIMIX_cond_destroy(smpi_host_cond());
+
        if (0 >= i) {
 
                // wake up senders/receivers
        if (0 >= i) {
 
                // wake up senders/receivers
@@ -172,12 +186,15 @@ int smpi_mpi_barrier(smpi_mpi_communicator_t comm)
 {
 
        SIMIX_mutex_lock(comm->barrier_mutex);
 {
 
        SIMIX_mutex_lock(comm->barrier_mutex);
-       if(++comm->barrier_count < comm->size) {
-               SIMIX_cond_wait(comm->barrier_cond, comm->barrier_mutex);
-       } else {
+       ++comm->barrier_count;
+       if (comm->barrier_count > comm->size) { // only happens on second barrier...
                comm->barrier_count = 0;
                comm->barrier_count = 0;
+       } else if (comm->barrier_count == comm->size) {
                SIMIX_cond_broadcast(comm->barrier_cond);
        }
                SIMIX_cond_broadcast(comm->barrier_cond);
        }
+       while (comm->barrier_count < comm->size) {
+               SIMIX_cond_wait(comm->barrier_cond, comm->barrier_mutex);
+       }
        SIMIX_mutex_unlock(comm->barrier_mutex);
 
        return MPI_SUCCESS;
        SIMIX_mutex_unlock(comm->barrier_mutex);
 
        return MPI_SUCCESS;
@@ -231,7 +248,7 @@ int smpi_mpi_wait(smpi_mpi_request_t request, smpi_mpi_status_t *status)
                retval = MPI_ERR_INTERN;
        } else {
                SIMIX_mutex_lock(request->mutex);
                retval = MPI_ERR_INTERN;
        } else {
                SIMIX_mutex_lock(request->mutex);
-               if (!request->completed) {
+               while (!request->completed) {
                        SIMIX_cond_wait(request->cond, request->mutex);
                }
                if (NULL != status) {
                        SIMIX_cond_wait(request->cond, request->mutex);
                }
                if (NULL != status) {