main.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include "aes.h"
  2. #include <mpi.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #define BUFFER_SIZE 4096
  7. #define PACKAGE_NUM 4096
  8. #define GLOG_ON false
  9. #if GLOG_ON
  10. #include <glog/logging.h>
  11. // pipeline macros
  12. #define PIPELINE_SEND(idx) \
  13. LOG(INFO) << "Proc" << rank << ": Isend started, idx:" << idx; \
  14. MPI_Isend(send_buffer[(idx) % 2], BUFFER_SIZE, MPI_UINT8_T, rank + 1, idx, \
  15. MPI_COMM_WORLD, &send_request); \
  16. LOG(INFO) << "Proc" << rank << ": Isend finished, idx:" << idx;
  17. #define PIPELINE_SEND_WAIT() \
  18. LOG(INFO) << "Proc" << rank << ": send wait started"; \
  19. MPI_Wait(&send_request, MPI_STATUS_IGNORE); \
  20. LOG(INFO) << "Proc" << rank << ": send wait finished";
  21. #define PIPELINE_RECV(idx) \
  22. LOG(INFO) << "Proc" << rank << ": Irecv started, idx:" << idx; \
  23. MPI_Irecv(recv_buffer[(idx) % 2], BUFFER_SIZE, MPI_UINT8_T, rank - 1, idx, \
  24. MPI_COMM_WORLD, &recv_request); \
  25. LOG(INFO) << "Proc" << rank << ": Irecv finished, idx:" << idx;
  26. #define PIPELINE_RECV_WAIT() \
  27. LOG(INFO) << "Proc" << rank << ": recv wait started"; \
  28. MPI_Wait(&recv_request, MPI_STATUS_IGNORE); \
  29. LOG(INFO) << "Proc" << rank << ": recv wait finished";
  30. // end pipeline macros
  31. #else
  32. // pipeline macros
  33. #define PIPELINE_SEND(idx) \
  34. MPI_Isend(send_buffer[(idx) % 2], BUFFER_SIZE, MPI_UINT8_T, rank + 1, idx, \
  35. MPI_COMM_WORLD, &send_request); \
  36. #define PIPELINE_SEND_WAIT() \
  37. MPI_Wait(&send_request, MPI_STATUS_IGNORE); \
  38. #define PIPELINE_RECV(idx) \
  39. MPI_Irecv(recv_buffer[(idx) % 2], BUFFER_SIZE, MPI_UINT8_T, rank - 1, idx, \
  40. MPI_COMM_WORLD, &recv_request); \
  41. #define PIPELINE_RECV_WAIT() \
  42. MPI_Wait(&recv_request, MPI_STATUS_IGNORE); \
  43. // end pipeline macros
  44. #endif
  45. // aes variables
  46. #define AES_ONCE 16
  47. uint8_t key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  48. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  49. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  50. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f};
  51. uint8_t *w;
  52. uint8_t in[AES_ONCE], out[AES_ONCE];
  53. // aes variables
  54. int main(int argc, char *argv[]) {
  55. int rank, size;
  56. MPI_Request send_request, recv_request;
  57. uint8_t send_buffer[2][BUFFER_SIZE], recv_buffer[2][BUFFER_SIZE];
  58. google::InitGoogleLogging(argv[0]);
  59. // MPI init
  60. MPI_Init(&argc, &argv);
  61. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  62. MPI_Comm_size(MPI_COMM_WORLD, &size);
  63. // end MPI init
  64. // aes init
  65. w = aes_init(sizeof(key));
  66. aes_key_expansion(key, w);
  67. // end aes init
  68. if (rank == 0) {
  69. // calculate(0)
  70. PIPELINE_SEND(0);
  71. for (int i = 1; i < PACKAGE_NUM; ++i) {
  72. // calculate(i)
  73. PIPELINE_SEND_WAIT();
  74. PIPELINE_SEND(i);
  75. }
  76. PIPELINE_SEND_WAIT();
  77. } else if (rank == 1) {
  78. PIPELINE_RECV(0);
  79. PIPELINE_RECV_WAIT();
  80. // calculate(0)
  81. PIPELINE_SEND(0);
  82. PIPELINE_RECV(1);
  83. for (int i = 1; i < PACKAGE_NUM - 1; ++i) {
  84. PIPELINE_RECV_WAIT();
  85. PIPELINE_RECV(i + 1);
  86. // calculate(i)
  87. PIPELINE_SEND_WAIT();
  88. PIPELINE_SEND(i);
  89. }
  90. PIPELINE_RECV_WAIT();
  91. // calculate(PAKAGE_NUM - 1)
  92. PIPELINE_SEND_WAIT();
  93. PIPELINE_SEND(PACKAGE_NUM - 1);
  94. PIPELINE_SEND_WAIT();
  95. } else if (rank == 2) {
  96. PIPELINE_RECV(0);
  97. for (int i = 1; i < PACKAGE_NUM; ++i) {
  98. PIPELINE_RECV_WAIT();
  99. PIPELINE_RECV(i);
  100. // calculate(i - 1)
  101. }
  102. PIPELINE_RECV_WAIT();
  103. // calculate(PACKAGE_NUM - 1)
  104. }
  105. MPI_Finalize();
  106. }