Skip to content
Snippets Groups Projects
Select Git revision
  • 02eb2b1b9c69821ec5a41f56a6a3e98221f21cef
  • main default protected
2 results

06_Les_conditions_partie2.ipynb

Blame
  • main.c 2.70 KiB
    #include "stdio.h"
    #include "stdlib.h"
    #include "grid.h"
    #include "mask.h"
    #include "node.h"
    #include "lib_gfx.h"
    #include <pthread.h>
    #include <semaphore.h>
    
    grid_t g;
    SDL_Keycode escape = 0;
    pthread_barrier_t b;
    int n_thread = 0;
    sem_t lock;
    pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
    pthread_mutex_t mutexcalc = PTHREAD_MUTEX_INITIALIZER;
    struct gfx_context_t *ctxt;
    void *keyboad_control(){
       
       while(1){
          sem_wait(&lock);
       escape = gfx_keypressed();
       if (escape == SDLK_ESCAPE)
       {
          
          exit(0);
          
       }
       sem_post(&lock);
       }
       
    }
    
    void *Calculate()
    {
       
       
       while (1)
       {
          
          grid_collide(&g);
          pthread_barrier_wait(&b);
          grid_propage(&g);
          pthread_barrier_wait(&b);
          
       }
       
    }
    void *Display(){
    
       sem_wait(&lock);
       while (1)
       {
          
          gfx_present(ctxt);
      
       }
       sem_post(&lock);
       
       
    } 
    int main(int argc, char *argv[])
    {
       
       
       int x = atoi(argv[1]), y = atoi(argv[2]);
       TAU = atof(argv[3]);
       G = atof(argv[4]);
       double Gy = atof(argv[5]);
       const double rho = 1.0;
       n_thread = atoi(argv[6]);
       pthread_barrier_init(&b, NULL,n_thread);
       int nthread_calcul = n_thread - 2;
       int rest, portion;
       pthread_t t_keyboard, t_display, t_calulator;
       sem_init(&lock, 0, n_thread);
       vect_t u = vect_new(0.02, 0.0);
       g = grid_create(x, y, rho, u);
       ctxt = gfx_create("Bolzmann", x, y);
       if (!ctxt)
       {
          fprintf(stderr, "Graphics initialization failed!\n");
          return EXIT_FAILURE;
       }
       portion = atoi(argv[1])/nthread_calcul;
       rest = atoi(argv[1])%nthread_calcul;
       
          
          SDL_PumpEvents();
          SDL_ShowCursor(SDL_ENABLE);
    
          g.x = portion;
          g.y = portion;
          for (int i = 0; i < nthread_calcul; i++)
          {
                if (i <= rest)
                   {
            
             g.x = g.x + 1;
             g.y = g.y + 1;
             
                    }
          
          pthread_create(&t_calulator, NULL, Calculate, NULL);
          
          
          g.x += portion;
          g.y += portion;
          }
          
          if (pthread_create(&t_keyboard, NULL, keyboad_control, NULL))
          {
             perror("pthread_create");
             return EXIT_FAILURE;
          }
          
          g.x = x;
          g.y = y;
          render(ctxt, &g);
          if (pthread_create(&t_display, NULL, Display, NULL))
          {
             perror("pthread_create");
             return EXIT_FAILURE;
          }
          
          gfx_left_right_mouse_pressed(&g);
          for (int i = 0; i < nthread_calcul; i++)
          {
              pthread_join(t_calulator, NULL);
          }
          
         
       
    
    
          pthread_join(t_display, NULL);
          pthread_join(t_keyboard, NULL);
          sem_destroy(&lock);
          pthread_barrier_destroy(&b);
          gfx_destroy(ctxt);
          grid_destroy(g);
    
       return EXIT_SUCCESS;
    }