main.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include "aes.h"
  2. #include <mpi.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #define BUFFER_SIZE 8192
  7. #define PACKAGE_NUM 12
  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. #define CALCULATE(idx) \
  31. LOG(INFO) << "Proc" << rank << "cal " << idx << " started"; \
  32. for (int cali = 0; cali < BUFFER_SIZE; cali += AES_ONCE) { \
  33. aes_cipher(&recv_buffer[(idx) % 2][cali], &send_buffer[(idx) % 2][cali], \
  34. w); \
  35. } \
  36. LOG(INFO) << "Proc" << rank << "cal " << idx << " finished";
  37. // end pipeline macros
  38. #else
  39. // pipeline macros
  40. #define PIPELINE_SEND(idx) \
  41. MPI_Isend(send_buffer[(idx) % 2], BUFFER_SIZE, MPI_UINT8_T, rank + 1, idx, \
  42. MPI_COMM_WORLD, &send_request);
  43. #define PIPELINE_SEND_WAIT() MPI_Wait(&send_request, MPI_STATUS_IGNORE);
  44. #define PIPELINE_RECV(idx) \
  45. MPI_Irecv(recv_buffer[(idx) % 2], BUFFER_SIZE, MPI_UINT8_T, rank - 1, idx, \
  46. MPI_COMM_WORLD, &recv_request);
  47. #define PIPELINE_RECV_WAIT() \
  48. MPI_Wait(&recv_request, MPI_STATUS_IGNORE); \
  49. #define CALCULATE(idx) \
  50. for (int cali = 0; cali < BUFFER_SIZE; cali += AES_ONCE) { \
  51. aes_cipher(&recv_buffer[(idx) % 2][cali], &send_buffer[(idx) % 2][cali], \
  52. w); \
  53. }
  54. // end pipeline macros
  55. #endif
  56. // aes variables
  57. #define AES_ONCE 16
  58. uint8_t key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  59. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  60. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  61. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f};
  62. uint8_t *w;
  63. uint8_t in[AES_ONCE], out[AES_ONCE];
  64. // aes variables
  65. int main(int argc, char *argv[]) {
  66. int rank, size;
  67. MPI_Request send_request, recv_request;
  68. uint8_t send_buffer[2][BUFFER_SIZE], recv_buffer[2][BUFFER_SIZE];
  69. #if GLOG_ON
  70. google::InitGoogleLogging(argv[0]);
  71. #endif
  72. // MPI init
  73. MPI_Init(&argc, &argv);
  74. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  75. MPI_Comm_size(MPI_COMM_WORLD, &size);
  76. // end MPI init
  77. // aes init
  78. w = aes_init(sizeof(key));
  79. aes_key_expansion(key, w);
  80. // end aes init
  81. if (rank == 0) {
  82. CALCULATE(0);
  83. PIPELINE_SEND(0);
  84. for (int i = 1; i < PACKAGE_NUM; ++i) {
  85. CALCULATE(i);
  86. PIPELINE_SEND_WAIT();
  87. PIPELINE_SEND(i);
  88. }
  89. PIPELINE_SEND_WAIT();
  90. } else if (rank == 1) {
  91. PIPELINE_RECV(0);
  92. PIPELINE_RECV_WAIT();
  93. CALCULATE(0);
  94. PIPELINE_SEND(0);
  95. PIPELINE_RECV(1);
  96. for (int i = 1; i < PACKAGE_NUM - 1; ++i) {
  97. PIPELINE_RECV_WAIT();
  98. PIPELINE_RECV(i + 1);
  99. CALCULATE(i);
  100. PIPELINE_SEND_WAIT();
  101. PIPELINE_SEND(i);
  102. }
  103. PIPELINE_RECV_WAIT();
  104. CALCULATE(PACKAGE_NUM - 1);
  105. PIPELINE_SEND_WAIT();
  106. PIPELINE_SEND(PACKAGE_NUM - 1);
  107. PIPELINE_SEND_WAIT();
  108. } else if (rank == 2) {
  109. PIPELINE_RECV(0);
  110. for (int i = 1; i < PACKAGE_NUM; ++i) {
  111. PIPELINE_RECV_WAIT();
  112. PIPELINE_RECV(i);
  113. CALCULATE(i - 1)
  114. }
  115. PIPELINE_RECV_WAIT();
  116. CALCULATE(PACKAGE_NUM - 1);
  117. }
  118. MPI_Finalize();
  119. }