teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

解決済み

2021/10/08 05:31

投稿

y_huto
y_huto

スコア3

title CHANGED
@@ -1,1 +1,1 @@
1
- c言語 MPI_Gatherを使用した二次元配列受け渡しに関する質問
1
+ 解決済みの質問です。
body CHANGED
@@ -1,123 +1,4 @@
1
- cにて、MPIを使用して並列計算を行い、二次元配列の受け渡すコードを実装したいと考えております。
1
+ 解決済み
2
2
 
3
- 以下のようなサンプルコードを作成したのですが、このままでは
4
- 確保していないメモリにアクセスしてしまいエラーを起こしてしまします。
5
- 恐らく/* localの行数決定 */の箇所のelse部分で、
6
- 一番最後のプロセスのtoの決定がうまく処理できていないことが原因なのではないかと考えております。
7
3
 
8
- 以下がソースコードになります。
9
- mpi_sample.c
10
- ```c
11
- #include <stdio.h>
12
- #include <stdlib.h>
13
-
14
- //mpi path
15
- #include "/usr/local/openmpi-4.1.1/include/mpi.h" /*if use local computer */
16
- //#include "mpi.h" /*if use super_computer*/
17
-
18
- void main(int argc, char **argv){
19
- int i, j, k;
20
- int l = 10;//global配列の長さ
21
- int row, from, to;
22
- double **ary_global, **ary_local;
23
-
24
- /* MPI Parameters */
25
- int myID, numProcs;
26
- MPI_Status mpi_status;
27
-
28
- /* MPI初期化 */
29
- MPI_Init(&argc, &argv);
30
- MPI_Comm_rank(MPI_COMM_WORLD, &myID);//プロセス番号
31
- MPI_Comm_size(MPI_COMM_WORLD, &numProcs);//プロセスのサイズ
32
-
33
- /* localの行数決定 */
34
- row = l / numProcs + 1;//from,toの幅
35
- from = row*myID;
36
- if(myID < numProcs - 1){
37
- to = row*(myID + 1) - 1;
38
- }else{//一番最後のPID
39
- to = l;
40
- }
41
-
42
- /* ary_local,grobal確保 */
43
- ary_local = (double **)malloc(sizeof(double *) * row);
44
- for(i = 0; i < row; i++) ary_local[i] = (double *)malloc(sizeof(double) * l);
45
- ary_global = (double **)malloc(sizeof(double *) * (row * numProcs));
46
- for(i = 0; i < row * numProcs; i++) ary_global[i] = (double *)malloc(sizeof(double) * l);
47
-
48
- /*globalに初期値-1を代入 */
49
- for(i = 0; i < row*numProcs; i++){
50
- for(j = 0; j < l; j++){
51
- ary_global[i][j] = -1;
52
- }
53
- }
54
-
55
- /* ary_localに値代入 */
56
- for(i = from; i < to; i++){
57
- for(j = 0; j < l; j ++){
58
- if(i == j){
59
- ary_local[i][j] = -1;
60
- }else{
61
- ary_local[i][j] = i*j;
62
- }
63
- }
64
- }
65
-
66
- /* localからglobalへ渡す */
67
- MPI_Gather(& ary_local[0][0], row * l, MPI_DOUBLE,
68
- & ary_global[0][0], row * l, MPI_DOUBLE, 0, MPI_COMM_WORLD);
69
-
70
- /* globalの内容表示 */
71
- if(myID == 0){
72
- for(i = 0; i < row * numProcs; i ++){
73
- for(j = 0; j < l; j ++){
74
- printf("%lf, ",ary_global[i][j]);
75
- }
76
- printf("\n");
77
- }
78
- }
79
-
80
- /* ary_local,grobal free() */
81
- for(i = 0; i < row; i++) free(ary_local[i]);
82
- free(ary_local);
83
-
84
- for(i = 0; i < row * numProcs; i++) free(ary_global[i]);
85
- free(ary_global);
86
-
87
- MPI_Barrier(MPI_COMM_WORLD);
88
- MPI_Finalize();
89
- }
90
-
91
- ```
92
-
93
- 以下がプロセス数2で実行した際の結果です。
94
- ```terminal
95
- $ mpicc -I/usr/local/openmpi-4.1.1/include -c mpi_sample.c
96
- $ mpicc -I/usr/local/openmpi-4.1.1/include -g mpi_sample.o -o mpi_sample
97
- $ mpirun -np 2 ./mpi_sample
98
- *** Process received signal ***
99
- Signal: Segmentation fault (11)
100
- Signal code: Address not mapped (1)
101
- Failing at address: (nil)
102
- [ 0] /lib/x86_64-linux-gnu/libc.so.6(+0x41100)[0x7f0ff7618100]
103
- [ 1] ./mpi(+0x1401)[0x560c4a75d401]
104
- [ 2] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xeb)[0x7f0ff75fb09b]
105
- [ 3] ./mpi(+0x111a)[0x560c4a75d11a]
106
- *** End of error message ***
107
- --------------------------------------------------------------------------
108
- Primary job terminated normally, but 1 process returned
109
- a non-zero exit code. Per user-direction, the job has been aborted.
110
- --------------------------------------------------------------------------
111
- --------------------------------------------------------------------------
112
- mpirun noticed that process rank 1 with PID 0 on node zenbook14 exited on signal 11 (Segmentation fault).
113
- --------------------------------------------------------------------------
114
- ```
115
-
116
-
117
- 実装したいことは、
118
- ary_local[row][l]で計算した結果を,ary_global[row * numProcs][l]にまとめるということです。
119
-
120
- MPIに疎いため、原因と考えている事とは別の原因があるのかもしれませんが、
121
- はわからなかっため質問させてただきました。
4
+ 己解決したの削除しまし、ありがとうございました。
122
-
123
- 申し訳ございませんが、改善案や別の良い実装方法があればお伺いしたいです。よろしくおねがいします。

1

誤字

2021/10/08 05:30

投稿

y_huto
y_huto

スコア3

title CHANGED
@@ -1,1 +1,1 @@
1
- c MPI_Gatherを使用した二次元配列の受け渡しに関する質問
1
+ c言語 MPI_Gatherを使用した二次元配列の受け渡しに関する質問
body CHANGED
@@ -1,11 +1,11 @@
1
- cにて、MPIを使用して並列計算を行って二次元配列の受け渡すコードを実装したいと考えております。
1
+ cにて、MPIを使用して並列計算を行い、二次元配列の受け渡すコードを実装したいと考えております。
2
2
 
3
3
  以下のようなサンプルコードを作成したのですが、このままでは
4
4
  確保していないメモリにアクセスしてしまいエラーを起こしてしまします。
5
5
  恐らく/* localの行数決定 */の箇所のelse部分で、
6
6
  一番最後のプロセスのtoの決定がうまく処理できていないことが原因なのではないかと考えております。
7
7
 
8
-
8
+ 以下がソースコードになります。
9
9
  mpi_sample.c
10
10
  ```c
11
11
  #include <stdio.h>
@@ -90,6 +90,7 @@
90
90
 
91
91
  ```
92
92
 
93
+ 以下がプロセス数2で実行した際の結果です。
93
94
  ```terminal
94
95
  $ mpicc -I/usr/local/openmpi-4.1.1/include -c mpi_sample.c
95
96
  $ mpicc -I/usr/local/openmpi-4.1.1/include -g mpi_sample.o -o mpi_sample