qsadfasdgfgads

Untitled

May 24th, 2023
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.06 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. #include <mpi.h>
  5. #include <math.h>
  6.  
  7. #define MAX_TASKS 5
  8. #define ITERATIONS_COUNT 100
  9. #define L 2
  10. #define REQUEST_TASK 228
  11. #define STOP_CODE 229
  12. #define TO_RECEIVER 1
  13. #define SEND_WORK_TAG 2
  14.  
  15. int *tasks;
  16. int size;
  17. int rank;
  18. int offset;
  19. pthread_mutex_t mutex;
  20.  
  21. int do_job(int length) {
  22.     int res = 0;
  23.     while (1) {
  24. //        if(rank == 0){
  25. //            printf("ZZZZZ %d %d\n", offset, length);
  26. //        }
  27.  
  28.         pthread_mutex_lock(&mutex);
  29.         if (offset >= length) {
  30.             pthread_mutex_unlock(&mutex);
  31.             break;
  32.         }
  33.         int current_offset = offset++;
  34.         pthread_mutex_unlock(&mutex);
  35.  
  36.         int weight = tasks[current_offset];
  37. //        if(rank == 0){
  38. //            printf("W %d\n", weight);
  39. //        }
  40.         for (int j = 0; j < weight; j++) {
  41.             res += (int)sqrt(j);
  42.         }
  43.     }
  44.     return res;
  45. }
  46.  
  47. void fill_tasks(int iter_count) {
  48.     for (int i = 0; i < size * MAX_TASKS; i++) {
  49.         tasks[i] = abs(50 - i % 100) * abs(rank - iter_count % size) * L;
  50.     }
  51. }
  52.  
  53. int do_request_tasks() {
  54.     for (int i = 0; i < size; i++) {
  55.         if (i == rank) continue;
  56.         int req_code = REQUEST_TASK;
  57.         int help_length;
  58.         //printf("VLASENKO DURAK ..vsdf.sd.fsd.f\n");
  59.         MPI_Send(&req_code, 1, MPI_INT, i, TO_RECEIVER, MPI_COMM_WORLD);
  60.         MPI_Recv(&help_length, 1, MPI_INT, i, SEND_WORK_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  61.         printf("!! resv length tasks VLASENKO TOP\n");
  62.         if (help_length > 0) {
  63.             MPI_Recv(tasks, help_length, MPI_INT, i, SEND_WORK_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  64.             printf("!!! recv tasks\n");
  65.             pthread_mutex_lock(&mutex);
  66.             offset = 0;
  67.             pthread_mutex_unlock(&mutex);
  68.         }
  69.  
  70.         return do_job(help_length);
  71.     }
  72. }
  73.  
  74.  
  75.  
  76. int do_tasks() {
  77.     int local_res = 0;
  78.     local_res = do_job(size * MAX_TASKS);
  79.  
  80.     local_res += do_request_tasks();
  81.     return local_res;
  82. }
  83.  
  84.  
  85.  
  86. void* worker_func(void* arg) {
  87.     for (int i = 0; i < ITERATIONS_COUNT; i++) {
  88.         pthread_mutex_lock(&mutex);
  89.         offset = 0;
  90.         fill_tasks(i);
  91.         pthread_mutex_unlock(&mutex);
  92.         int res = do_tasks();
  93.         if (rank == 0) {
  94.             printf("%d\n", res);
  95.         }
  96.     }
  97.     int stop_code = STOP_CODE;
  98.     MPI_Send(&stop_code, 1, MPI_INT, 0, TO_RECEIVER, MPI_COMM_WORLD);
  99.     return NULL;
  100. }
  101.  
  102. void* receiver_func(void* arg) {
  103.     while (1) {
  104.         int req_code_buf;
  105.         MPI_Status status_worker_requester;
  106.         MPI_Recv(&req_code_buf, 1, MPI_INT, MPI_ANY_SOURCE, TO_RECEIVER, MPI_COMM_WORLD, &status_worker_requester);
  107.  
  108.         if (req_code_buf == STOP_CODE) break;
  109.  
  110.         size_t length = size * MAX_TASKS;
  111.         int new_offset = offset + (int)((int)(length - offset) * 0.3);
  112.         int tasks_length = new_offset - offset;
  113.         printf("ASDDSA 1 %d\n", tasks_length);
  114.         MPI_Send(&tasks_length, 1, MPI_INT, status_worker_requester.MPI_SOURCE, SEND_WORK_TAG, MPI_COMM_WORLD);
  115.         printf("ASDDSA 2\n");
  116.         if (tasks_length > 0 ) {
  117.             pthread_mutex_lock(&mutex);
  118.             int old_offset = offset;
  119.             offset = new_offset;
  120.             pthread_mutex_unlock(&mutex);
  121.             printf("ASDDSA 3\n");
  122.             MPI_Send(&tasks[old_offset], tasks_length, MPI_INT, status_worker_requester.MPI_SOURCE, SEND_WORK_TAG, MPI_COMM_WORLD);
  123.             printf("ASDDSA 4\n");
  124.         }
  125.     }
  126.     return NULL;
  127. }
  128.  
  129. int main(int argc, char* argv[]) {
  130.     MPI_Init_thread(NULL, NULL, MPI_THREAD_MULTIPLE, NULL);
  131.     MPI_Comm_size(MPI_COMM_WORLD, &size);
  132.     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  133.     tasks = malloc(MAX_TASKS * sizeof(int));
  134.     pthread_t worker, receiver;
  135.     pthread_mutex_init(&mutex, NULL);
  136.     pthread_create(&worker, NULL, worker_func, NULL);
  137.     pthread_create(&receiver, NULL, receiver_func, NULL);
  138.  
  139.     pthread_join(worker, NULL);
  140.     pthread_join(receiver, NULL);
  141.  
  142.     free(tasks);
  143.     MPI_Finalize();
  144.     return 0;
  145. }
Add Comment
Please, Sign In to add comment