mpi/{send_recv_urd/send_recv_urd.cpp → stdin_sendrecv/stdin_sendrecv.cpp} RENAMED
@@ -1,50 +1,57 @@
1
  /*
2
  * Copyright 2021 Jeisson Hidalgo-Cespedes - Universidad de Costa Rica
3
  * Creates a secondary thread that greets in the standard output
4
  */
5
 
6
  #include <mpi.h>
7
- #include <cstdint>
8
  #include <iostream>
9
- #include <string>
10
- #include <sstream>
11
  #include <vector>
12
 
13
- const size_t MESSAGE_CAPACITY = 256;
14
-
15
  int main(int argc, char* argv[]) {
16
  if (MPI_Init(&argc, &argv) == MPI_SUCCESS) {
17
  int my_rank = -1;
18
  MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
19
 
20
  int process_count = -1;
21
  MPI_Comm_size(MPI_COMM_WORLD, &process_count);
22
 
23
  char hostname[MPI_MAX_PROCESSOR_NAME];
24
  int hostname_len = -1;
25
  MPI_Get_processor_name(hostname, &hostname_len);
26
 
27
- std::ostringstream buffer;
28
- buffer << "Hello from main thread of process " << my_rank
29
- << " of " << process_count << " on " << hostname;
30
-
31
- if (my_rank != 0) {
32
- const std::string& message = buffer.str();
33
- MPI_Send(message.data(), /*count*/ message.length() + 1, MPI_SIGNED_CHAR
34
- , /*dest*/ 0, /*tag*/ 0, MPI_COMM_WORLD);
35
- } else {
36
- std::cout << 0 << " says: " << buffer.str() << std::endl;
37
- std::vector<char> message(256, '\0');
38
- for (int sender = 1; sender < process_count; ++sender) {
39
- MPI_Recv(&message[0], /*capacity*/ message.size(), MPI_SIGNED_CHAR
40
- , MPI_ANY_SOURCE, /*tag*/ 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
41
- std::cout << sender << " says: " << &message[0] << std::endl;
42
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  }
44
 
45
  MPI_Finalize();
46
  } else {
47
  std::cout << "Error: could not init MPI environment" << std::endl;
48
  }
49
  return 0;
50
  }
1
  /*
2
  * Copyright 2021 Jeisson Hidalgo-Cespedes - Universidad de Costa Rica
3
  * Creates a secondary thread that greets in the standard output
4
  */
5
 
6
  #include <mpi.h>
 
7
  #include <iostream>
8
+ #include <cstdint>
 
9
  #include <vector>
10
 
 
 
11
  int main(int argc, char* argv[]) {
12
  if (MPI_Init(&argc, &argv) == MPI_SUCCESS) {
13
  int my_rank = -1;
14
  MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
15
 
16
  int process_count = -1;
17
  MPI_Comm_size(MPI_COMM_WORLD, &process_count);
18
 
19
  char hostname[MPI_MAX_PROCESSOR_NAME];
20
  int hostname_len = -1;
21
  MPI_Get_processor_name(hostname, &hostname_len);
22
 
23
+ std::vector<int64_t> numbers;
24
+
25
+ if (my_rank == 0) {
26
+ int64_t number = 0;
27
+ while (std::cin >> number) {
28
+ numbers.push_back(number);
 
 
 
 
 
 
 
 
 
29
  }
30
+
31
+ size_t number_count = numbers.size();
32
+ for (int dest = 1; dest < process_count; ++dest) {
33
+ MPI_Send(&number_count, /*count*/ 1, MPI_UINT64_T, dest, /*tag*/ 0
34
+ , MPI_COMM_WORLD);
35
+ MPI_Send(&numbers[0], number_count, MPI_INT64_T, dest, /*tag*/ 0
36
+ , MPI_COMM_WORLD);
37
+ }
38
+ }
39
+ else {
40
+ size_t number_count = numbers.size();
41
+ MPI_Recv(&number_count, /*capacity*/ 1, MPI_UINT64_T
42
+ , /*source*/ 0, /*tag*/ 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
43
+ numbers.resize(number_count);
44
+ MPI_Recv(&numbers[0], /*capacity*/ number_count, MPI_INT64_T
45
+ , 0, /*tag*/ 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
46
+ }
47
+
48
+ if (numbers.size() > 1) {
49
+ std::cout << "Process " << my_rank << " has " << numbers[0] << std::endl;
50
  }
51
 
52
  MPI_Finalize();
53
  } else {
54
  std::cout << "Error: could not init MPI environment" << std::endl;
55
  }
56
  return 0;
57
  }