質問編集履歴

4

注意された点を修正しました。

2019/07/19 09:21

投稿

oidatoho
oidatoho

スコア12

test CHANGED
File without changes
test CHANGED
@@ -12,307 +12,275 @@
12
12
 
13
13
  ```
14
14
 
15
- error: 'N' undeclared here (not in a function)
15
+ error: conflicting types for 'search'
16
16
 
17
17
  void search(int a, int b, int array[N/2])
18
18
 
19
- error: conflicting types for 'search'
19
+ note: previous declaration of 'search' was here
20
+
21
+ int search(int, int, int[]);
22
+
23
+ ```
24
+
25
+ 環境はLinuxです。 
26
+
27
+ mpicc tsuyoi.c(プログラム名) -o tsuyoi -lm
28
+
29
+ でコンパイルしようとしたところ、このエラーがでした。
30
+
31
+
32
+
33
+ ### 該当のソースコード
34
+
35
+
36
+
37
+ ```C
38
+
39
+ #include <mpi.h>
40
+
41
+ #include <math.h>
42
+
43
+ #include <stdio.h>
44
+
45
+ #include <string.h>
46
+
47
+ #define N 50000
48
+
49
+
50
+
51
+ int main(int argv, char* argc[])
52
+
53
+ {
54
+
55
+          int n, base_n, source;
56
+
57
+          int low1, low2, high1, high2;
58
+
59
+          int i, a;
60
+
61
+          int size, rank, length;
62
+
63
+          int low_array[n];
64
+
65
+          int high_array[n];
66
+
67
+          int num;
68
+
69
+
70
+
71
+          int search(int, int, int[]);
72
+
73
+
74
+
75
+          double starttime, endtime;
76
+
77
+
78
+
79
+          MPI_Status status;
80
+
81
+
82
+
83
+          n = N;
84
+
85
+
86
+
87
+          MPI_Init(&argv, &argc);
88
+
89
+
90
+
91
+          starttime = MPI_Wtime();
92
+
93
+
94
+
95
+          MPI_Comm_size(MPI_COMM_WORLD, &size);
96
+
97
+          MPI_Comm_rank(MPI_COMM_WORLD, &rank);
98
+
99
+
100
+
101
+          for(i = 0; i < n; i++)
102
+
103
+          {
104
+
105
+              low_array[i] = 0;
106
+
107
+              high_array[i] = 0;
108
+
109
+          }
110
+
111
+
112
+
113
+          a = 0;
114
+
115
+          num = 0;
116
+
117
+
118
+
119
+          if(rank != 0)
120
+
121
+          {
122
+
123
+             base_n = n / ((size - 1)* 2);
124
+
125
+             low1 = base_n*(rank - 1)+ 1;
126
+
127
+             low2 = base_n * rank;
128
+
129
+             high1 = n - base_n * rank + 1;
130
+
131
+             high2 = n - base_n * (rank -1);
132
+
133
+
134
+
135
+             search(low1, low2, low_array);
136
+
137
+
138
+
139
+             search(high1, high2, high_array);
140
+
141
+
142
+
143
+             MPI_Send(&low_array, n, MPI_INT, 0, 0, MPI_COMM_WORLD);
144
+
145
+             MPI_Send(&high_array, n, MPI_INT, 0, 0, MPI_COMM_WORLD);
146
+
147
+
148
+
149
+           }
150
+
151
+           else
152
+
153
+           {
154
+
155
+                for(source = 1; source < size; source++)
156
+
157
+                {
158
+
159
+                    MPI_Recv(&low_array, n, MPI_INT, source, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
160
+
161
+
162
+
163
+                     while(low_array[i] != 0)
164
+
165
+                     {
166
+
167
+                           num = num + 1;
168
+
169
+                           i++;
170
+
171
+                     }
172
+
173
+
174
+
175
+                     i = 0;
176
+
177
+
178
+
179
+                    MPI_Recv(&high_array, n, MPI_INT, source, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
180
+
181
+
182
+
183
+                     while(high_array[i] != 0)
184
+
185
+                     {
186
+
187
+                           num++;
188
+
189
+                           i++;
190
+
191
+                     }
192
+
193
+
194
+
195
+                     i = 0;
196
+
197
+                }
198
+
199
+
200
+
201
+            endtime = MPI_Wtime();
202
+
203
+
204
+
205
+            printf("%dまでの素数の数は%dです。\n", n, num-1);
206
+
207
+            printf("%lf\n", (endtime - starttime));
208
+
209
+
210
+
211
+           }
212
+
213
+
214
+
215
+           MPI_Finalize();
216
+
217
+
218
+
219
+ }
220
+
221
+
20
222
 
21
223
  void search(int a, int b, int array[N/2])
22
224
 
23
- note: previous declaration of 'search' was here
225
+ {
24
-
226
+
25
- int search(int, int, int[]);
227
+ int i, j, frag, now;
228
+
229
+ frag = 0;
230
+
231
+ now = 0;
232
+
233
+
234
+
235
+ for(i = a; i <= b; i++)
236
+
237
+ {
238
+
239
+ for(j = 2; j <= i/2; j++)
240
+
241
+ {
242
+
243
+ if(i % j == 0)
244
+
245
+
246
+
247
+ frag = 1;
248
+
249
+ }
250
+
251
+
252
+
253
+ if(frag == 0)
254
+
255
+ {
256
+
257
+ array[now] = i;
258
+
259
+ now++;
260
+
261
+ }
262
+
263
+
264
+
265
+ frag = 0;
266
+
267
+ }
268
+
269
+
270
+
271
+ }
26
272
 
27
273
  ```
28
274
 
29
- 環境はLinuxです。 
30
-
31
- mpicc tsuyoi.c(プログラム名) -o tsuyoi -lm
32
-
33
- でコンパイルしようとしたところ、このエラーがでした。
34
-
35
-
36
-
37
- ### 該当のソースコード
38
-
39
-
40
-
41
- ```C
42
-
43
- #include <mpi.h>
44
-
45
- #include <math.h>
46
-
47
- #include <stdio.h>
48
-
49
- #include <string.h>
50
-
51
-
52
-
53
- int main(int argv, char* argc[])
54
-
55
- {
56
-
57
-          int n, N, base_n, source;
58
-
59
-          int low1, low2, high1, high2;
60
-
61
-          int i, a;
62
-
63
-          int size, rank, length;
64
-
65
-          int low_array[n];
66
-
67
-          int high_array[n];
68
-
69
-          int num;
70
-
71
-
72
-
73
-          int search(int, int, int[]);
74
-
75
-
76
-
77
-          double starttime, endtime;
78
-
79
-          char hostname[MPI_MAX_PROCESSOR_NAME];
80
-
81
-
82
-
83
-          MPI_Status status;
84
-
85
-
86
-
87
-          n = N;
88
-
89
-
90
-
91
-          MPI_Init(&argv, &argc);
92
-
93
-
94
-
95
-          starttime = MPI_Wtime();
96
-
97
-
98
-
99
-          MPI_Comm_size(MPI_COMM_WORLD, &size);
100
-
101
-          MPI_Comm_rank(MPI_COMM_WORLD, &rank);
102
-
103
-          MPI_Get_processor_name(hostname, &length);
104
-
105
-
106
-
107
-          if (rank == 0)
108
-
109
-          {
110
-
111
-     printf("\n");
112
-
113
-      printf("#######################################################");
114
-
115
-     printf("\n\n");
116
-
117
-     printf("Master node name: %s\n", hostname);
118
-
119
-     printf("\n");
120
-
121
-     printf("3以上の整数を入力してください:\n");
122
-
123
-     printf("\n");
124
-
125
-     scanf("%d",&n);
126
-
127
-     printf("\n");
128
-
129
-          }
130
-
131
-
132
-
133
-          for(i = 0; i < n; i++)
134
-
135
-          {
136
-
137
-              low_array[i] = 0;
138
-
139
-              high_array[i] = 0;
140
-
141
-          }
142
-
143
-
144
-
145
-          a = 0;
146
-
147
-          num = 0;
148
-
149
-
150
-
151
-          if(rank != 0)
152
-
153
-          {
154
-
155
-             base_n = n / ((size - 1)* 2);
156
-
157
-             low1 = base_n*(rank - 1)+ 1;
158
-
159
-             low2 = base_n * rank;
160
-
161
-             high1 = n - base_n * rank + 1;
162
-
163
-             high2 = n - base_n * (rank -1);
164
-
165
-
166
-
167
-             search(low1, low2, low_array);
168
-
169
-
170
-
171
-             search(high1, high2, high_array);
172
-
173
-
174
-
175
-             MPI_Send(&low_array, n, MPI_INT, 0, 0, MPI_COMM_WORLD);
176
-
177
-             MPI_Send(&high_array, n, MPI_INT, 0, 0, MPI_COMM_WORLD);
178
-
179
-
180
-
181
-           }
182
-
183
-           else
184
-
185
-           {
186
-
187
-                for(source = 1; source < size; source++)
188
-
189
-                {
190
-
191
-                    MPI_Recv(&low_array, n, MPI_INT, source, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
192
-
193
-
194
-
195
-                     while(low_array[i] != 0)
196
-
197
-                     {
198
-
199
-                           num = num + 1;
200
-
201
-                           i++;
202
-
203
-                     }
204
-
205
-
206
-
207
-                     i = 0;
208
-
209
-
210
-
211
-                    MPI_Recv(&high_array, n, MPI_INT, source, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
212
-
213
-
214
-
215
-                     while(high_array[i] != 0)
216
-
217
-                     {
218
-
219
-                           num++;
220
-
221
-                           i++;
222
-
223
-                     }
224
-
225
-
226
-
227
-                     i = 0;
228
-
229
-                }
230
-
231
-
232
-
233
-            endtime = MPI_Wtime();
234
-
235
-
236
-
237
-            printf("%dまでの素数の数は%dです。\n", n, num-1);
238
-
239
-            printf("%lf\n", (endtime - starttime));
240
-
241
-
242
-
243
-           }
244
-
245
-
246
-
247
-           MPI_Finalize();
248
-
249
-
250
-
251
- }
252
-
253
-
254
-
255
- void search(int a, int b, int array[N/2])
256
-
257
- {
258
-
259
- int i, j, frag, now;
260
-
261
- frag = 0;
262
-
263
- now = 0;
264
-
265
-
266
-
267
- for(i = a; i <= b; i++)
268
-
269
- {
270
-
271
- for(j = 2; j <= i/2; j++)
272
-
273
- {
274
-
275
- if(i % j == 0)
276
-
277
-
278
-
279
- frag = 1;
280
-
281
- }
282
-
283
-
284
-
285
- if(frag == 0)
286
-
287
- {
288
-
289
- array[now] = i;
290
-
291
- now++;
292
-
293
- }
294
-
295
-
296
-
297
- frag = 0;
298
-
299
- }
300
-
301
-
302
-
303
- }
304
-
305
- ```
306
-
307
275
 
308
276
 
309
277
  ### 試したこと
310
278
 
311
279
 
312
280
 
313
- エラー名をそのまま検索したりしましたが、Nが定義していないと出ました。
281
+ エラー名をそのまま検索したりしました.
314
-
315
- 定義できていないのでしょうか。
282
+
283
+
316
284
 
317
285
 
318
286
 
@@ -320,9 +288,7 @@
320
288
 
321
289
 
322
290
 
323
- こちらのpdfに乗っているプログラムを変えて作成しました。
291
+ こちらのpdfに乗っているプログラムを使用しました。
324
-
325
-
326
292
 
327
293
  http://www.oyama-ct.ac.jp/tosyo/kiyou/kiyou40/014nansaikiyomi.pdf
328
294
 

3

タイトルを変更しました。

2019/07/19 09:21

投稿

oidatoho
oidatoho

スコア12

test CHANGED
@@ -1 +1 @@
1
- Searchに対する定義されていない参照です エラー
1
+ 素数をカウントするプログラムのエラーについて。
test CHANGED
File without changes

2

コードを編集し、新たなエラーを書きました。 よろしくお願いします。

2019/07/19 08:09

投稿

oidatoho
oidatoho

スコア12

test CHANGED
File without changes
test CHANGED
@@ -12,7 +12,17 @@
12
12
 
13
13
  ```
14
14
 
15
+ error: 'N' undeclared here (not in a function)
16
+
17
+ void search(int a, int b, int array[N/2])
18
+
19
+ error: conflicting types for 'search'
20
+
21
+ void search(int a, int b, int array[N/2])
22
+
23
+ note: previous declaration of 'search' was here
24
+
15
- 'search'に対する定義されていない参照です
25
+ int search(int, int, int[]);
16
26
 
17
27
  ```
18
28
 
@@ -44,7 +54,7 @@
44
54
 
45
55
  {
46
56
 
47
-          int n, base_n, source;
57
+          int n, N, base_n, source;
48
58
 
49
59
           int low1, low2, high1, high2;
50
60
 
@@ -74,7 +84,7 @@
74
84
 
75
85
 
76
86
 
77
-          n;
87
+          n = N;
78
88
 
79
89
 
80
90
 
@@ -240,6 +250,58 @@
240
250
 
241
251
  }
242
252
 
253
+
254
+
255
+ void search(int a, int b, int array[N/2])
256
+
257
+ {
258
+
259
+ int i, j, frag, now;
260
+
261
+ frag = 0;
262
+
263
+ now = 0;
264
+
265
+
266
+
267
+ for(i = a; i <= b; i++)
268
+
269
+ {
270
+
271
+ for(j = 2; j <= i/2; j++)
272
+
273
+ {
274
+
275
+ if(i % j == 0)
276
+
277
+
278
+
279
+ frag = 1;
280
+
281
+ }
282
+
283
+
284
+
285
+ if(frag == 0)
286
+
287
+ {
288
+
289
+ array[now] = i;
290
+
291
+ now++;
292
+
293
+ }
294
+
295
+
296
+
297
+ frag = 0;
298
+
299
+ }
300
+
301
+
302
+
303
+ }
304
+
243
305
  ```
244
306
 
245
307
 
@@ -248,9 +310,9 @@
248
310
 
249
311
 
250
312
 
251
- エラー名をそのまま検索したりしましたが、mainど他の関数ではでるのですが、
313
+ エラー名をそのまま検索したりしましたが、Nが定義していいと出ました。
252
-
314
+
253
- search関数は出ませんで
315
+ 定義できていないのでしょうか
254
316
 
255
317
 
256
318
 

1

URLの追加

2019/07/19 08:07

投稿

oidatoho
oidatoho

スコア12

test CHANGED
File without changes
test CHANGED
@@ -260,4 +260,10 @@
260
260
 
261
261
  こちらのpdfに乗っているプログラムを少し変えて作成しました。
262
262
 
263
+
264
+
265
+ http://www.oyama-ct.ac.jp/tosyo/kiyou/kiyou40/014nansaikiyomi.pdf
266
+
267
+
268
+
263
269
  よろしくお願いします。