examples: add a basic usage of mpi
authorAlexis Laferrière <alexis.laf@xymus.net>
Tue, 1 Jul 2014 19:12:37 +0000 (15:12 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Tue, 8 Jul 2014 17:24:42 +0000 (13:24 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

examples/mpi/Makefile [new file with mode: 0644]
examples/mpi/src/mpi_simple.nit [new file with mode: 0644]
tests/sav/mpi_simple.res [new file with mode: 0644]

diff --git a/examples/mpi/Makefile b/examples/mpi/Makefile
new file mode 100644 (file)
index 0000000..09212ae
--- /dev/null
@@ -0,0 +1,8 @@
+bin/mpi_simple:
+       mkdir -p bin/
+       ../../bin/nitg src/mpi_simple.nit -o bin/mpi_simple
+
+run:
+       mpiexec.openmpi --hostfile hosts bin/mpi_simple
+
+.PHONY: bin/mpi_simple
diff --git a/examples/mpi/src/mpi_simple.nit b/examples/mpi/src/mpi_simple.nit
new file mode 100644 (file)
index 0000000..4691e18
--- /dev/null
@@ -0,0 +1,107 @@
+# This file is part of NIT (http://www.nitlanguage.org).
+#
+# Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+module mpi_simple
+
+import mpi
+
+# Simple class transfered between processors
+class ProcessorInfo
+       auto_serializable
+       super Serializable
+
+       var rank: Int
+       var size: Int
+       var name: String
+       var string_of_random_length: String
+
+       init(mpi: MPI)
+       do
+               self.rank = comm_world.rank.to_i
+               self.size = comm_world.size
+               self.name = mpi.processor_name
+               self.string_of_random_length = "+" * 10.rand
+       end
+
+       redef fun to_s do return "<{name}: {rank}/{size} {string_of_random_length}>"
+end
+
+var mpi = new MPI
+
+var data = new CIntArray(2)
+if comm_world.size == 1 then
+       print "not enough nodes, got only 1"
+       mpi.finalize
+       exit 1
+end
+
+print "stdout: processor '{mpi.processor_name}' {comm_world.rank}/{comm_world.size}"
+
+if comm_world.rank == 0.rank then
+       # send - ints
+       data[0] = 123
+       data[1] = 456
+       mpi.send_from(data, 0, 2, 1.rank, 0.tag, comm_world)
+
+       # send - simple string
+       mpi.send_all("Hello", 1.rank, 0.tag, comm_world)
+       mpi.send_from(" World", 0, 6, 1.rank, 0.tag, comm_world)
+       mpi.send_from("+-!0?", 2, 2, 1.rank, 0.tag, comm_world)
+else if comm_world.rank == 1.rank then
+
+       # recv - ints
+       mpi.recv_into(data, 0, 2, 0.rank, 0.tag, comm_world)
+       print "received data: {data[0]} {data[1]}"
+
+       # recv - simple string
+       var buf = new FlatBuffer.with_capacity(5)
+       mpi.recv_fill(buf, 0.rank, 0.tag, comm_world)
+       print "received string: {buf}"
+
+       mpi.recv_into(buf, 5, 6, 0.rank, 0.tag, comm_world)
+       print "received string: {buf}"
+
+       mpi.recv_into(buf, 3, 2, 0.rank, 0.tag, comm_world)
+       print "received string: {buf}"
+end
+
+# Passing complex objects and inverse sender/receiver
+if comm_world.rank == 0.rank then
+       var errors = 0
+       var processors_per_host = new HashMap[String, Int]
+
+       # recv - serializable
+       for p in [1 .. comm_world.size[ do
+               var a = mpi.recv(new Rank.any, 0.tag, comm_world)
+               print "received serialized: {a or else "<null>"}"
+
+               if a != null and a isa ProcessorInfo then
+                       if not processors_per_host.keys.has(a.name) then
+                               processors_per_host[a.name] = 1
+                       else processors_per_host[a.name] += 1
+               else errors += 1
+       end
+
+       print "errors: {errors}"
+       print "processors: {processors_per_host.join(", ", ": ")}"
+else
+       # send - serializable
+       srand_from comm_world.rank.to_i
+       var a = new ProcessorInfo(mpi)
+       mpi.send(a, 0.rank, 0.tag, comm_world)
+end
+
+mpi.finalize
diff --git a/tests/sav/mpi_simple.res b/tests/sav/mpi_simple.res
new file mode 100644 (file)
index 0000000..c5e5fc6
--- /dev/null
@@ -0,0 +1 @@
+not enough nodes, got only 1