Implementation of bcast 61/8861/1
Sylvestre Ledru [Wed, 29 Aug 2012 13:17:44 +0000 (15:17 +0200)]
Change-Id: Icac5afaeaf718ce1d00ad305c94365ea68239fe0

scilab/modules/mpi/help/en_US/MPI_Bcast.xml [new file with mode: 0644]
scilab/modules/mpi/sci_gateway/c/sci_mpi_bcast.c
scilab/modules/mpi/tests/unit_tests/mpi_bcast.tst [new file with mode: 0644]

diff --git a/scilab/modules/mpi/help/en_US/MPI_Bcast.xml b/scilab/modules/mpi/help/en_US/MPI_Bcast.xml
new file mode 100644 (file)
index 0000000..ff58690
--- /dev/null
@@ -0,0 +1,74 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+ * Copyright (C) 2011 - DIGITEO - Sylvestre Ledru
+ * 
+ -->
+<refentry xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svg="http://www.w3.org/2000/svg" xmlns:ns3="http://www.w3.org/1999/xhtml" xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:db="http://docbook.org/ns/docbook" version="5.0-subset Scilab" xml:id="MPI_Send" xml:lang="en">
+    <refnamediv>
+        <refname>MPI_Bcast</refname>
+        <refpurpose>Broadcasts a message from the process with rank root to all other processes of the group.</refpurpose>
+    </refnamediv>
+    <refsynopsisdiv>
+        <title>Calling Sequence</title>
+        <synopsis>res = MPI_Bcast(value,rootID)</synopsis>
+    </refsynopsisdiv>
+    <refsection>
+        <title>Arguments</title>
+        <variablelist>
+            <varlistentry>
+                <term>value</term>
+                <listitem>
+                    <para>The value to be sent to all nodes.</para>
+                </listitem>
+            </varlistentry>
+            <varlistentry>
+                <term>rootID</term>
+                <listitem>
+                    <para>The source of the message</para>
+                </listitem>
+            </varlistentry>
+            <varlistentry>
+                <term>res</term>
+                <listitem>
+                    <para>The broadcasted value</para>
+                </listitem>
+            </varlistentry>
+        </variablelist>
+    </refsection>
+    <refsection>
+        <title>Description</title>
+        <para>
+            Send a variable to all nodes. The value should be retrieved by the node with the function <link linkend="MPI_Recv">MPI_Recv</link>.
+        </para>
+        <para>On the internal technical side, two broadcasts are used transparently. The first one sends the size of the data, the second the data themself.
+        </para>
+    </refsection>
+    <refsection>
+        <title>Examples</title>
+        <programlisting role="example">
+MPI_Init();
+rnk =    MPI_Comm_rank();
+sizeNodes =    MPI_Comm_size();
+
+SLV = rnk;                // handy shortcuts, master is rank 0
+Master = ~ SLV;            // slaves are all other
+a=-1
+if Master
+   // Declare a
+   a=42
+end
+b=MPI_Bcast(a, 0);
+assert_checkequal(b,42);
+MPI_Finalize();
+        </programlisting>
+    </refsection>
+    <refsection role="see also">
+        <title>See Also</title>
+        <simplelist type="inline">
+            <member>
+                <link linkend="MPI_Recv">MPI_Recv</link>
+            </member>
+        </simplelist>
+    </refsection>
+</refentry>
index 68f429a..bfdfcc4 100644 (file)
@@ -18,8 +18,6 @@
 #include "localization.h"
 #include "serialization.h"
 
-#define TAG 0
-
 int sci_mpi_bcast(char *fname, unsigned long fname_len)
 {
     SciErr sciErr;
@@ -88,25 +86,55 @@ int sci_mpi_bcast(char *fname, unsigned long fname_len)
 
     if (iRet)
     {
+        printf("pas reussi a seraliser\n");
     }
+    int rank, length;
+    MPI_Comm_rank(MPI_COMM_WORLD,&rank);
+
+    /* First, send the size of the data as broadcast */
+    iRet = MPI_Bcast(&iBufferSize, 1, MPI_INT, rootID, MPI_COMM_WORLD);
 
+    /* Second, restore the data with the right size */
     iRet = MPI_Bcast(piBuffer, iBufferSize, MPI_INT, rootID, MPI_COMM_WORLD);
+
     if (iRet != MPI_SUCCESS)
     {
         char error_string[MPI_MAX_ERROR_STRING];
         int length_of_error_string;
 
+        printf("ICI \n"); fflush(NULL);
         MPI_Error_string(iRet, error_string, &length_of_error_string);
-        Scierror("%s: Could not send the variable to the node %d: %s\n", fname, rootID, error_string);
+        Scierror("%s: Could not broadcast the variable to the node %d: %s\n", fname, rootID, error_string);
         return 1;
     }
 
-    free(piBuffer);
-    if (createScalarBoolean(pvApiCtx, 1, !iRet))
+    switch (piBuffer[0])
     {
+    case sci_matrix:
+        iRet = deserialize_double(pvApiCtx, piBuffer, iBufferSize);
+        break;
+    case sci_strings:
+        iRet = deserialize_string(pvApiCtx, piBuffer, iBufferSize);
+        break;
+    case sci_boolean:
+        iRet = deserialize_boolean(pvApiCtx, piBuffer, iBufferSize);
+        break;
+    case sci_sparse:
+        iRet = deserialize_sparse(pvApiCtx, piBuffer, iBufferSize, TRUE);
+        break;
+    case sci_boolean_sparse:
+        iRet = deserialize_sparse(pvApiCtx, piBuffer, iBufferSize, FALSE);
+        break;
+    case sci_ints:
+        iRet = deserialize_int(pvApiCtx, piBuffer, iBufferSize);
+        break;
+    default:
         return 1;
+        break;
     }
 
+//    free(piBuffer);
+
     LhsVar(1) = 1;
     PutLhsVar();
     return 0;
diff --git a/scilab/modules/mpi/tests/unit_tests/mpi_bcast.tst b/scilab/modules/mpi/tests/unit_tests/mpi_bcast.tst
new file mode 100644 (file)
index 0000000..5b49ec8
--- /dev/null
@@ -0,0 +1,40 @@
+// =============================================================================
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2010-2010 - DIGITEO - Sylvestre LEDRU
+//
+//  This file is distributed under the same license as the Scilab package.
+// =============================================================================
+//
+// <-- MPI TEST 4 -->
+//
+MPI_Init();
+rnk =    MPI_Comm_rank();
+sizeNodes =    MPI_Comm_size();
+
+assert_checkequal(MPI_Comm_size(), 4);
+
+SLV = rnk;                // handy shortcuts, master is rank 0
+Master = ~ SLV;            // slaves are all other
+
+a=-1
+if Master
+   // Declare a
+   a=42
+end
+b=MPI_Bcast(a, 0);
+assert_checkequal(b,42);
+
+a2=-1
+
+if Master
+   // Declare a
+   a2=[2,2;3,2];
+
+end
+
+b2=MPI_Bcast(a2, 0);
+assert_checkequal(b2,[2,2;3,2]);
+
+
+MPI_Finalize();
+exit
\ No newline at end of file