Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.54 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <stdbool.h>
  5. #include <pthread.h>
  6. #include <unistd.h>
  7.  
  8. static int x1, y1, x2, y2;
  9. static int x3, y3;
  10. pthread_mutex_t counterMutex;
  11. int rowCounter;
  12.  
  13. typedef struct {
  14.         bool **m1;
  15.         bool **m2;
  16.         bool **m3;
  17. } Thread_struct;
  18.  
  19. void createMatrix(bool **m1, bool **m2, bool **m3) {
  20.         int zarodek;
  21.         time_t tt;
  22.         zarodek = time(&tt);
  23.         srand(zarodek);
  24.         bool m2Temp[x2][y2];
  25.  
  26.         bool **tempPP = m1;
  27.         for (int i=0; i<x1; i++) {
  28.                 bool *tempP = *tempPP;
  29.                 for (int k=0; k<y1; k++) {
  30.                         *tempP = (bool)(rand()%2);
  31.                         printf("%d ", *tempP);
  32.                         tempP++;
  33.                 }
  34.                 tempPP++;
  35.                 printf("\n");
  36.         }
  37.  
  38.         printf("\n");
  39.  
  40.         for (int i=0; i<x2; i++) {
  41.                 for (int k=0; k<y2; k++) {
  42.                         m2Temp[i][k] = (bool)(rand()%2);
  43.                         printf("%d ", m2Temp[i][k]);
  44.                 }
  45.                 printf("\n");
  46.         }
  47.  
  48.         printf("\n");
  49.  
  50.         tempPP = m2;
  51.         for (int i=0; i<y2; i++) { // TRANSPOZYCJA
  52.                 bool *tempP = *tempPP;
  53.                 for (int k=0; k<x2; k++) {
  54.                         *tempP = m2Temp[k][i];
  55.                         printf("%d ", *tempP);
  56.                         tempP++;
  57.                 }
  58.                 tempPP++;
  59.                 printf(" T \n");
  60.         }
  61.  
  62.         printf("\n");
  63.  
  64.         tempPP = m3;
  65.         for (int i=0; i<x3; i++) { // TRANSPOZYCJA
  66.                 bool *tempP = *tempPP;
  67.                 for (int k=0; k<y2; k++) {
  68.                         *tempP = false;
  69.                         printf("%d ", *tempP);
  70.                         tempP++;
  71.                 }
  72.                 tempPP++;
  73.                 printf(" 3 \n");
  74.         }
  75.  
  76.         printf("\n");
  77. }
  78.  
  79. void *matrixCalc(void *arg) {
  80.         int row = 0;
  81.         Thread_struct *t = (Thread_struct *) arg;
  82.  
  83.         while (true) {
  84.                 pthread_mutex_lock(&counterMutex);
  85.                 row = rowCounter;
  86.                 rowCounter++;
  87.                 pthread_mutex_unlock(&counterMutex);
  88.  
  89.                 if (row > x1) {
  90.                         return NULL;
  91.                 }
  92.  
  93.                 bool *m3 = *((t->m3) + row);
  94.                 bool *m1 = *((t->m1) + row);
  95.                 bool **m2 = (t->m2);
  96.                 for (int i=0; i<y2; i++) {
  97.                         bool *tempP = *m2;
  98.                         for (int k=0; k<x2; k++) { // x2=y1
  99.                                 if (*m1 && *tempP) {
  100.                                         *m3 = true;
  101.                                         continue;
  102.                                 }
  103.                                 tempP++;
  104.                         }
  105.                         m1++;
  106.                         m3++;
  107.                         m2++;
  108.                 }
  109.         }
  110.         return NULL;
  111. }
  112.  
  113. int main () {
  114.         int nthreads = 0;
  115.         printf("x1: ");
  116.         scanf("%d", &x1);
  117.         printf("y1, x2: ");
  118.         scanf("%d", &y1);
  119.         x2 = y1;
  120.         printf("y2: ");
  121.         scanf("%d", &y2);
  122.         x3 = x1;
  123.         y3 = y2;
  124.  
  125.         bool **m1 = (bool **)malloc(sizeof(bool *) * x1);
  126.         for (int i=0; i<x1; i++) {
  127.                 m1[i] = (bool*)malloc(sizeof(bool) * y1);
  128.         }
  129.  
  130.         bool **m2 = (bool **)malloc(sizeof(bool *) * y2);
  131.         for (int i=0; i<y2; i++) {
  132.                 m2[i] = (bool*)malloc(sizeof(bool) * x2);
  133.         }
  134.  
  135.         bool **m3 = (bool **)malloc(sizeof(bool *) * x3);
  136.         for (int i=0; i<x3; i++) {
  137.                 m3[i] = (bool*)malloc(sizeof(bool) * y3);
  138.         }
  139.  
  140.         createMatrix(m1, m2, m3);
  141.  
  142.         Thread_struct t;
  143.         t.m1 = m1;
  144.         t.m2 = m2;
  145.         t.m3 = m3;
  146.  
  147.         // Tutaj zaczyna sie zabawa
  148.         printf("Insert amount of threads: ");
  149.         scanf("%d", &nthreads);
  150.         rowCounter = 0;
  151.  
  152.         pthread_t rowCalculator[nthreads];
  153.         for (int i=0; i<nthreads; i++) {
  154.                 if (pthread_create(&rowCalculator[i], NULL, matrixCalc, &t)) {
  155.                         printf("error in thread creation");
  156.                         abort(); // fatal error exit
  157.                 }
  158.         }
  159.  
  160.         for (int i=0; i<nthreads; i++) {
  161.                 if (pthread_join(rowCalculator[i], NULL)) {
  162.                         printf("error in thread ending");
  163.                         abort();
  164.                 }
  165.         }
  166.  
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement