Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
finally putting in proper use of mutexes. code still needs work.
authormarkls <markls@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Tue, 3 Jul 2007 20:01:01 +0000 (20:01 +0000)
committermarkls <markls@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Tue, 3 Jul 2007 20:01:01 +0000 (20:01 +0000)
git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@3649 48e7efb5-ca39-0410-a469-dd3cf9ba447f

src/simix/smx_synchro.c
src/smpi/Makefile
src/smpi/include/smpi.h
src/smpi/scripts/smpicc
src/smpi/scripts/smpirun
src/smpi/src/smpi_base.c

index 4e71510..9451f5a 100644 (file)
@@ -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;
index c22adbd..7d914a4 100644 (file)
@@ -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)
index 3a7426d..adcc689 100644 (file)
@@ -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;
 };
index 62435f4..2a4899d 100755 (executable)
@@ -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} "
index 69abe80..a5e4c6e 100755 (executable)
@@ -36,8 +36,8 @@ done
 EXEC="$1"
 shift
 
-PLATFORMTMP="$(mktemp tmpXXXXXX)"
-#PLATFORMTMP="pla.xml"
+#PLATFORMTMP="$(mktemp tmpXXXXXX)"
+PLATFORMTMP="pla.xml"
 
 cat > ${PLATFORMTMP} <<PLATFORMHEAD
 <?xml version='1.0'?>
@@ -65,8 +65,8 @@ cat >> ${PLATFORMTMP} <<PLATFORMFOOT
 </platform_description>
 PLATFORMFOOT
 
-APPLICATIONTMP="$(mktemp tmpXXXXXX)"
-#APPLICATIONTMP="app.xml"
+#APPLICATIONTMP="$(mktemp tmpXXXXXX)"
+APPLICATIONTMP="app.xml"
 
 cat > ${APPLICATIONTMP} <<APPLICATIONHEAD
 <?xml version='1.0'?>
@@ -75,24 +75,21 @@ cat > ${APPLICATIONTMP} <<APPLICATIONHEAD
 APPLICATIONHEAD
 
 for (( i=${NUMPROCS}; $i ; i=$i-1 )) do
-  echo "  <process host=\"host$i\" function=\"smpi_main\">" >> ${APPLICATIONTMP}
+  echo "  <process host=\"host$i\" function=\"smpi_simulated_main\">" >> ${APPLICATIONTMP}
   for ARG in $*; do
     echo "    <argument value=\"${ARG}\"/>" >> ${APPLICATIONTMP}
   done
   echo "  </process>" >> ${APPLICATIONTMP}
 done
 
-for (( i=${NUMPROCS}; $i ; i=$i-1 )) do
-  echo "  <process host=\"host$i\" function=\"smpi_sender\"/>" >> ${APPLICATIONTMP}
-done
-
-for (( i=${NUMPROCS}; $i ; i=$i-1 )) do
-  echo "  <process host=\"host$i\" function=\"smpi_receiver\"/>" >> ${APPLICATIONTMP}
-done
+#for (( i=${NUMPROCS}; $i ; i=$i-1 )) do
+#  echo "  <process host=\"host$i\" function=\"smpi_sender\"/>" >> ${APPLICATIONTMP}
+#  echo "  <process host=\"host$i\" function=\"smpi_receiver\"/>" >> ${APPLICATIONTMP}
+#done
 
 cat >> ${APPLICATIONTMP} <<APPLICATIONFOOT
 </platform_description>
 APPLICATIONFOOT
 
 ${EXEC} ${PLATFORMTMP} ${APPLICATIONTMP}
-rm ${PLATFORMTMP} ${APPLICATIONTMP}
+#rm ${PLATFORMTMP} ${APPLICATIONTMP}
index 6528100..5bdfe11 100644 (file)
@@ -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;
 }