From 136aada204feb49032c48dd0467dcacb1fdd50a4 Mon Sep 17 00:00:00 2001 From: markls Date: Tue, 3 Jul 2007 20:01:01 +0000 Subject: [PATCH] finally putting in proper use of mutexes. code still needs work. git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@3649 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- src/simix/smx_synchro.c | 4 +-- src/smpi/Makefile | 6 ++-- src/smpi/include/smpi.h | 2 ++ src/smpi/scripts/smpicc | 2 +- src/smpi/scripts/smpirun | 23 ++++++------ src/smpi/src/smpi_base.c | 78 +++++++++++++++++++++++----------------- 6 files changed, 64 insertions(+), 51 deletions(-) diff --git a/src/simix/smx_synchro.c b/src/simix/smx_synchro.c index 4e715105d5..9451f5a294 100644 --- a/src/simix/smx_synchro.c +++ b/src/simix/smx_synchro.c @@ -96,9 +96,9 @@ int SIMIX_mutex_trylock(smx_mutex_t mutex) void SIMIX_mutex_unlock(smx_mutex_t mutex) { smx_process_t p; /*process to wake up */ - + xbt_assert0((mutex != NULL), "Invalid parameters"); - + if (xbt_swag_size(mutex->sleeping) > 0) { p = xbt_swag_extract(mutex->sleeping); mutex->using = 0; diff --git a/src/smpi/Makefile b/src/smpi/Makefile index c22adbd3c0..7d914a4c55 100644 --- a/src/smpi/Makefile +++ b/src/smpi/Makefile @@ -1,5 +1,5 @@ CC=gcc #-DDEBUG -CFLAGS=-O3 +CFLAGS=-g SRCDIR=./src BINDIR=./bin LIBDIR=./lib @@ -16,10 +16,10 @@ $(LIBDIR): mkdir -p $(LIBDIR) $(BUILDDIR)/smpi_base.o: src/smpi_base.c include/smpi.h Makefile $(BUILDDIR) - $(CC) $(CFLAGS) -c -I ../include -I $(INCLUDEDIR) -I ${SIMGRID_HOME}/include src/smpi_base.c -o $(BUILDDIR)/smpi_base.o + $(CC) $(CFLAGS) -c -I .. -I ../include -I $(INCLUDEDIR) -I ${SIMGRID_HOME}/include src/smpi_base.c -o $(BUILDDIR)/smpi_base.o $(BUILDDIR)/smpi_mpi.o: src/smpi_mpi.c include/smpi.h Makefile $(BUILDDIR) - $(CC) $(CFLAGS) -c -I ../include -I $(INCLUDEDIR) -I ${SIMGRID_HOME}/include src/smpi_mpi.c -o $(BUILDDIR)/smpi_mpi.o + $(CC) $(CFLAGS) -c -I .. -I ../include -I $(INCLUDEDIR) -I ${SIMGRID_HOME}/include src/smpi_mpi.c -o $(BUILDDIR)/smpi_mpi.o $(BUILDDIR): mkdir -p $(BUILDDIR) diff --git a/src/smpi/include/smpi.h b/src/smpi/include/smpi.h index 3a7426d8a3..adcc689621 100644 --- a/src/smpi/include/smpi.h +++ b/src/smpi/include/smpi.h @@ -23,6 +23,8 @@ struct smpi_mpi_communicator_t { int size; int barrier; + smx_mutex_t barrier_mutex; + smx_cond_t barrier_cond; smx_host_t *hosts; smx_process_t *processes; }; diff --git a/src/smpi/scripts/smpicc b/src/smpi/scripts/smpicc index 62435f4090..2a4899d22b 100755 --- a/src/smpi/scripts/smpicc +++ b/src/smpi/scripts/smpicc @@ -51,7 +51,7 @@ while [ -n "$1" ]; do LINKARGS="" CMDLINE="${CMDLINE} -c " elif [ "${ARG%.c}" != "${ARG}" ]; then - INCLUDEARGS="-I ../include -I${SMPI_INCLUDE} -I${SIMGRID_INCLUDE} " + INCLUDEARGS="-I .. -I ../include -I${SMPI_INCLUDE} -I${SIMGRID_INCLUDE} " SRCFILE="$(realpath ${ARG})" modsource ${SRCFILE} CMDLINE="${CMDLINE} ${TMPDIR}${SRCFILE} " diff --git a/src/smpi/scripts/smpirun b/src/smpi/scripts/smpirun index 69abe808c5..a5e4c6e8de 100755 --- a/src/smpi/scripts/smpirun +++ b/src/smpi/scripts/smpirun @@ -36,8 +36,8 @@ done EXEC="$1" shift -PLATFORMTMP="$(mktemp tmpXXXXXX)" -#PLATFORMTMP="pla.xml" +#PLATFORMTMP="$(mktemp tmpXXXXXX)" +PLATFORMTMP="pla.xml" cat > ${PLATFORMTMP} < @@ -65,8 +65,8 @@ cat >> ${PLATFORMTMP} < PLATFORMFOOT -APPLICATIONTMP="$(mktemp tmpXXXXXX)" -#APPLICATIONTMP="app.xml" +#APPLICATIONTMP="$(mktemp tmpXXXXXX)" +APPLICATIONTMP="app.xml" cat > ${APPLICATIONTMP} < @@ -75,24 +75,21 @@ cat > ${APPLICATIONTMP} <" >> ${APPLICATIONTMP} + echo " " >> ${APPLICATIONTMP} for ARG in $*; do echo " " >> ${APPLICATIONTMP} done echo " " >> ${APPLICATIONTMP} done -for (( i=${NUMPROCS}; $i ; i=$i-1 )) do - echo " " >> ${APPLICATIONTMP} -done - -for (( i=${NUMPROCS}; $i ; i=$i-1 )) do - echo " " >> ${APPLICATIONTMP} -done +#for (( i=${NUMPROCS}; $i ; i=$i-1 )) do +# echo " " >> ${APPLICATIONTMP} +# echo " " >> ${APPLICATIONTMP} +#done cat >> ${APPLICATIONTMP} < APPLICATIONFOOT ${EXEC} ${PLATFORMTMP} ${APPLICATIONTMP} -rm ${PLATFORMTMP} ${APPLICATIONTMP} +#rm ${PLATFORMTMP} ${APPLICATIONTMP} diff --git a/src/smpi/src/smpi_base.c b/src/smpi/src/smpi_base.c index 6528100bf6..5bdfe118a5 100644 --- a/src/smpi/src/smpi_base.c +++ b/src/smpi/src/smpi_base.c @@ -29,14 +29,17 @@ static double smpi_reference_speed; XBT_LOG_NEW_DEFAULT_CATEGORY(smpi, "SMPI"); -void smpi_sender() +// mutexes +smx_mutex_t smpi_running_hosts_mutex; + +int smpi_sender(int argc, char **argv) { - return; + return 0; } -void smpi_receiver() +int smpi_receiver(int argc, char **argv) { - return; + return 0; } int smpi_run_simulation(int argc, char **argv) @@ -116,9 +119,6 @@ void smpi_mpi_init() smx_host_t host; double duration; - // FIXME: mutex? - smpi_running_hosts++; - // initialize some local variables host = SIMIX_host_self(); hosts = SIMIX_host_get_table(); @@ -127,26 +127,31 @@ void smpi_mpi_init() // node 0 sets the globals if (host == hosts[0]) { + // mutexes + smpi_running_hosts_mutex = SIMIX_mutex_init(); + // global communicator - smpi_mpi_comm_world.size = size; - smpi_mpi_comm_world.barrier = 0; - smpi_mpi_comm_world.hosts = hosts; - smpi_mpi_comm_world.processes = xbt_new0(smx_process_t, size); - smpi_mpi_comm_world.processes[0] = SIMIX_process_self(); + smpi_mpi_comm_world.size = size; + smpi_mpi_comm_world.barrier = 0; + smpi_mpi_comm_world.barrier_mutex = SIMIX_mutex_init(); + smpi_mpi_comm_world.barrier_cond = SIMIX_cond_init(); + smpi_mpi_comm_world.hosts = hosts; + smpi_mpi_comm_world.processes = xbt_new0(smx_process_t, size); + smpi_mpi_comm_world.processes[0] = SIMIX_process_self(); // mpi datatypes - smpi_mpi_byte.size = (size_t)1; - smpi_mpi_int.size = sizeof(int); - smpi_mpi_double.size = sizeof(double); + smpi_mpi_byte.size = (size_t)1; + smpi_mpi_int.size = sizeof(int); + smpi_mpi_double.size = sizeof(double); // mpi operations - smpi_mpi_land.func = &smpi_mpi_land_func; - smpi_mpi_sum.func = &smpi_mpi_sum_func; + smpi_mpi_land.func = &smpi_mpi_land_func; + smpi_mpi_sum.func = &smpi_mpi_sum_func; // smpi globals - smpi_pending_send_requests = xbt_new0(xbt_fifo_t, size); - smpi_pending_recv_requests = xbt_new0(xbt_fifo_t, size); - smpi_received_messages = xbt_new0(xbt_fifo_t, size); + smpi_pending_send_requests = xbt_new0(xbt_fifo_t, size); + smpi_pending_recv_requests = xbt_new0(xbt_fifo_t, size); + smpi_received_messages = xbt_new0(xbt_fifo_t, size); for(i = 0; i < size; i++) { smpi_pending_send_requests[i] = xbt_fifo_new(); @@ -168,27 +173,37 @@ void smpi_mpi_init() // FIXME: signal node 0 // FIXME: wait for node 0 } + + SIMIX_mutex_lock(smpi_running_hosts_mutex); + smpi_running_hosts++; + SIMIX_mutex_lock(smpi_running_hosts_mutex); } void smpi_mpi_finalize() { int i; - // FIXME: mutex? - smpi_running_hosts--; + SIMIX_mutex_lock(smpi_running_hosts_mutex); + i = --smpi_running_hosts; + SIMIX_mutex_unlock(smpi_running_hosts_mutex); + + if (0 >= i) { + + SIMIX_mutex_destroy(smpi_running_hosts_mutex); - if (0 >= smpi_running_hosts) { for (i = 0 ; i < smpi_mpi_comm_world.size; i++) { xbt_fifo_free(smpi_pending_send_requests[i]); xbt_fifo_free(smpi_pending_recv_requests[i]); xbt_fifo_free(smpi_received_messages[i]); } + xbt_free(smpi_pending_send_requests); xbt_free(smpi_pending_recv_requests); xbt_free(smpi_received_messages); xbt_free(smpi_mpi_comm_world.processes); xbt_os_timer_free(smpi_timer); } + } void smpi_bench_begin() @@ -212,18 +227,15 @@ void smpi_bench_end() void smpi_barrier(smpi_mpi_communicator_t *comm) { int i; - // FIXME: mutex + SIMIX_mutex_lock(comm->barrier_mutex); comm->barrier++; - if(comm->barrier < comm->size) { - SIMIX_process_suspend(SIMIX_process_self()); + if(i < comm->size) { + SIMIX_cond_wait(comm->barrier_cond, comm->barrier_mutex); } else { comm->barrier = 0; - for(i = 0; i < comm->size; i++) { - if (SIMIX_process_is_suspended(comm->processes[i])) { - SIMIX_process_resume(comm->processes[i]); - } - } + SIMIX_cond_broadcast(comm->barrier_cond); } + SIMIX_mutex_unlock(comm->barrier_mutex); } int smpi_comm_rank(smpi_mpi_communicator_t *comm, smx_host_t host) @@ -260,6 +272,7 @@ unsigned int smpi_sleep(unsigned int seconds) // FIXME: simix sleep self = SIMIX_host_self(); sleep_action = SIMIX_action_sleep(self, seconds); + sleep(seconds); smpi_bench_begin(); return 0; } @@ -267,8 +280,9 @@ unsigned int smpi_sleep(unsigned int seconds) void smpi_exit(int status) { smpi_bench_end(); - // FIXME: mutex + SIMIX_mutex_lock(smpi_running_hosts_mutex); smpi_running_hosts--; + SIMIX_mutex_unlock(smpi_running_hosts_mutex); SIMIX_process_kill(SIMIX_process_self()); return; } -- 2.20.1