14.6.2. Hello World Example 2ΒΆ
- The source code is shown below, which can be downloaded
here
. - Run the source code with 4 processors
mpiexec -np 4 python hello2.py
the outputs look like
Random:
Hello from 2
Hello from 1
Hello from 3
Ordered:
Hello from 1
Hello from 2
Hello from 3
Broadcasting:
Hello from 0
Hello from 0
Hello from 0
Process 3 Terminating
Process 2 Terminating
Process 1 Terminating
Process 0 Terminating
The script is shown below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | import openseespy.opensees as ops pid = ops.getPID() np = ops.getNP() # datatype = 'float' # datatype = 'int' datatype = 'str' if pid == 0: print('Random: ') for i in range(1, np): data = ops.recv('-pid', 'ANY') print(data) else: if datatype == 'str': ops.send('-pid', 0, 'Hello from {}'.format(pid)) elif datatype == 'float': ops.send('-pid', 0, float(pid)) elif datatype == 'int': ops.send('-pid', 0, int(pid)) ops.barrier() if pid == 0: print('\nOrdered: ') for i in range(1, np): data = ops.recv('-pid', i) print(data) else: if datatype == 'str': ops.send('-pid', 0, 'Hello from {}'.format(pid)) elif datatype == 'float': ops.send('-pid', 0, float(pid)) elif datatype == 'int': ops.send('-pid', 0, int(pid)) ops.barrier() if pid == 0: print('\nBroadcasting: ') if datatype == 'str': ops.Bcast('Hello from {}'.format(pid)) elif datatype == 'float': ops.Bcast(float(pid)) elif datatype == 'int': ops.Bcast(int(pid)) else: data = ops.Bcast() print(data) |