mpi/{stdin_sendrecv/stdin_sendrecv.cpp → stdin_wtime/stdin_wtime.cpp} RENAMED
@@ -1,57 +1,61 @@
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
  }
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 <iomanip>
8
  #include <iostream>
9
  #include <cstdint>
10
  #include <vector>
11
 
12
  int main(int argc, char* argv[]) {
13
  if (MPI_Init(&argc, &argv) == MPI_SUCCESS) {
14
  int my_rank = -1;
15
  MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
16
 
17
  int process_count = -1;
18
  MPI_Comm_size(MPI_COMM_WORLD, &process_count);
19
 
20
  char hostname[MPI_MAX_PROCESSOR_NAME];
21
  int hostname_len = -1;
22
  MPI_Get_processor_name(hostname, &hostname_len);
23
 
24
  std::vector<int64_t> numbers;
25
+ double start_time = MPI_Wtime();
26
 
27
  if (my_rank == 0) {
28
  int64_t number = 0;
29
  while (std::cin >> number) {
30
  numbers.push_back(number);
31
  }
32
 
33
  size_t number_count = numbers.size();
34
  for (int dest = 1; dest < process_count; ++dest) {
35
  MPI_Send(&number_count, /*count*/ 1, MPI_UINT64_T, dest, /*tag*/ 0
36
  , MPI_COMM_WORLD);
37
  MPI_Send(&numbers[0], number_count, MPI_INT64_T, dest, /*tag*/ 0
38
  , MPI_COMM_WORLD);
39
  }
40
  }
41
  else {
42
  size_t number_count = numbers.size();
43
  MPI_Recv(&number_count, /*capacity*/ 1, MPI_UINT64_T
44
  , /*source*/ 0, /*tag*/ 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
45
  numbers.resize(number_count);
46
  MPI_Recv(&numbers[0], /*capacity*/ number_count, MPI_INT64_T
47
  , 0, /*tag*/ 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
48
  }
49
 
50
+ double elapsed = MPI_Wtime() - start_time;
51
+
52
+ std::cout << "Process " << my_rank << " got " << numbers.size()
53
+ << " numbers in " << std::fixed << std::setprecision(9) << elapsed
54
+ << "s" << std::endl;
55
 
56
  MPI_Finalize();
57
  } else {
58
  std::cout << "Error: could not init MPI environment" << std::endl;
59
  }
60
  return 0;
61
  }