Exemple #1
0
static void * thr_fn0(void *arg)
{
	printids("new thread0: ");
	char input_buf1[INPUT_LEN];
	char input_buf2[INPUT_LEN];
	int i;
	int str_len;
	int sent;
	comm_create(device1, nblks, blksz);
	comm_create(device2, nblks, blksz);

	/*read input from standard I/O, one line at a time, input_buf2 reverses input_buf1.*/
	while(NULL != fgets(input_buf1, INPUT_LEN-1, stdin))
	{
		str_len = strlen(input_buf1);
		input_buf1[str_len - 1] = '\0';
		str_len = strlen(input_buf1);
		for(i = 0; i < str_len; ++i)
		{
			input_buf2[i] = input_buf1[str_len-1-i];
		}
		input_buf2[i] = '\0';
		comm_info("Thread0 send input1:%s, length is:%d\n", input_buf1, str_len);
		sent = comm_send(device1, input_buf1, str_len);
		comm_info("Thread0 sent length is:%d\n", sent);

		comm_info("Thread0 send input2:%s, length is:%d\n", input_buf2, str_len);
		sent = comm_send(device2, input_buf2, str_len);
		comm_info("Thread0 sent length is:%d\n", sent);
	}
}
Exemple #2
0
static void allreduce_imp(const struct comm *com, gs_dom dom, gs_op op,
                          void *v, uint vn, void *buf)
{
    size_t total_size = vn*gs_dom_size[dom];
    const uint id=com->id, np=com->np;
    uint n = np, c=1, odd=0, base=0;
    while(n>1) {
        odd=(odd<<1)|(n&1);
        c<<=1, n>>=1;
        if(id>=base+n) c|=1, base+=n, n+=(odd&1);
    }
    while(n<np) {
        if(c&1) n-=(odd&1), base-=n;
        c>>=1, n<<=1, n+=(odd&1);
        odd>>=1;
        if(base==id) {
            comm_recv(com, buf,total_size, id+n/2,id+n/2);
            gs_gather_array(v,buf,vn, dom,op);
        } else {
            comm_send(com, v,total_size, base,id);
            break;
        }
    }
    while(n>1) {
        if(base==id)
            comm_send(com, v,total_size, id+n/2,id);
        else
            comm_recv(com, v,total_size, base,base);
        odd=(odd<<1)|(n&1);
        c<<=1, n>>=1;
        if(id>=base+n) c|=1, base+=n, n+=(odd&1);
    }
}
Exemple #3
0
int game_load(int slot) {
	int retval;
	char temp[80];
	board_t *board;

	if (ch_userdir()) {
		DBG_ERROR("Failed to enter user directory");
		return 1;
	}

	comm_send("force\n");

	snprintf(temp, sizeof(temp), "save%i.pgn", slot);
	retval = pgn_parse_file(temp);

	if (retval) {
		DBG_ERROR("Failed to parse PGN file '%s'", temp);
		return 1;
	}

	board = history->last->board;

	ui->update(board, NULL);

	if (config->player[board->turn] == PLAYER_ENGINE)
		comm_send("go\n");
	else if (config->player[OPPONENT(board->turn)] == PLAYER_ENGINE) {
		if (board->turn == WHITE)
			comm_send("white\n");
		else
			comm_send("black\n");
	}

	return retval;
}
int oemp_SendData(WORD wDevID, BYTE* pBuf, int nSize)
{
	WORD wChkSum = 0;
	BYTE Buf[4], *pCommBuf;
	int nSentBytes;

	if (pBuf == NULL)
		return PKT_PARAM_ERR;

	Buf[0] = (BYTE)STX3;
	Buf[1] = (BYTE)STX4;
	*((WORD*)(&Buf[SB_OEM_HEADER_SIZE])) = wDevID;

	wChkSum = oemp_CalcChkSumOfDataPkt(Buf, SB_OEM_HEADER_SIZE + SB_OEM_DEV_ID_SIZE);
	wChkSum += oemp_CalcChkSumOfDataPkt(pBuf, nSize);

	pCommBuf = new BYTE[nSize + SB_OEM_HEADER_SIZE + SB_OEM_DEV_ID_SIZE + SB_OEM_CHK_SUM_SIZE];
	memcpy(pCommBuf, Buf, SB_OEM_HEADER_SIZE + SB_OEM_DEV_ID_SIZE);
	memcpy(pCommBuf + SB_OEM_HEADER_SIZE + SB_OEM_DEV_ID_SIZE, pBuf, nSize);
	*(WORD*)(pCommBuf + nSize + SB_OEM_HEADER_SIZE + SB_OEM_DEV_ID_SIZE) = wChkSum;

	nSentBytes = comm_send(pCommBuf, nSize + SB_OEM_HEADER_SIZE + SB_OEM_DEV_ID_SIZE + SB_OEM_CHK_SUM_SIZE, gCommTimeOut);
	if (nSentBytes != nSize + SB_OEM_HEADER_SIZE + SB_OEM_DEV_ID_SIZE + SB_OEM_CHK_SUM_SIZE)
	{
		if (pCommBuf)
			delete pCommBuf;
		return PKT_COMM_ERR;
	}

	if (pCommBuf)
		delete pCommBuf;

	return 0;
}
/*
 * int
 * slave_fd_probe(int sock);
 * -------------------------
 *  Probe the slave at the other end of 'sock'.
 *
 *  Mandatory params: sock
 *  Optional params :
 *
 *  Return values:
 *   -1 Error
 *    0 Success
 */
int
slave_fd_probe(int sock){
	// Local variables
	synexec_msg_t           net_msg;                // synexec msg
	int                     err = 0;                // Return code

	// Probe the slave
	if (comm_send(sock, MT_SYNEXEC_MSG_PROBE, NULL, NULL, 0) <= 0){
		goto err;
	}

	// Process the reply
	if (comm_recv(sock, &net_msg, NULL, NULL, NULL) <= 0){
		goto err;
	}
	if (net_msg.command != MT_SYNEXEC_MSG_REPLY){
		goto err;
	}

out:
	// Return
	return(err);

err:
	err = -1;
	goto out;
}
Exemple #6
0
void game_move_now(void) {
	/* Make sure engine is on move. */
	if (config->player[history->last->board->turn] != PLAYER_ENGINE)
		return;

	comm_send("?\n");
}
Exemple #7
0
void game_make_move(move_t *move, int ui_update) {
	if (do_move(move, ui_update)) {
		comm_send("%s\n", fullalg_list.move[fullalg_list.entries - 1]);
	} else {
		char *move_str = move_to_fullalg(history->last->board, move);
		DBG_WARN("Ignoring illegal move %s", move_str);
		free(move_str);
	}
}
Exemple #8
0
/* Send an error message */
int comm_senderr(int fd, char *errcode, char *errmsg, struct addr *addr,
                 int flags) {
    /* Prepare message */
    char *fields[] = { "", errcode, errmsg };
    struct ctlmsg msg = CTLMSG_INIT;
    msg.fieldnum = 3;
    msg.fields = fields;
    /* Deliver message */
    return comm_send(fd, &msg, addr, flags);
}
Exemple #9
0
void game_retract_move(void) {
	/* Make sure a user is on move and we can undo two moves. */
	if (config->player[history->last->board->turn] != PLAYER_UI)
		return;
	if (!history->last->prev || !history->last->prev->prev)
		return;

	game_undo();
	game_undo();
	comm_send("remove\n");
}
Exemple #10
0
int main()
{
	COMM_ERR comm_status;
	int len = BUFFER_LEN;

	board_init();
	comm_init();

	comm_send(":SSBOOT", 7);
	comm_status = comm_recv(buf, &len, BOOT_TIMEOUT_MS);

	if (comm_status == COMM_ERR_TIMEOUT || buf[0] != 'i')
	{
		comm_send("NOK", 3);
		while(1);
	}

	comm_send("OK", 2);


	while(1);

	return 0;
}
Exemple #11
0
static void scan_imp(void *scan, const struct comm *com, gs_dom dom, gs_op op,
                     const void *v, uint vn, void *buffer)
{
    comm_req req[2];
    size_t vsize = vn*gs_dom_size[dom];
    const uint id=com->id, np=com->np;
    uint n = np, c=1, odd=0, base=0;
    void *buf[2];
    void *red = (char*)scan+vsize;
    buf[0]=buffer,buf[1]=(char*)buffer+vsize;
    while(n>1) {
        odd=(odd<<1)|(n&1);
        c<<=1, n>>=1;
        if(id>=base+n) c|=1, base+=n, n+=(odd&1);
    }
    gs_init_array(scan,vn,dom,op);
    memcpy(red,v,vsize);
    while(n<np) {
        if(c&1) n-=(odd&1), base-=n;
        c>>=1, n<<=1, n+=(odd&1);
        odd>>=1;
        if(base==id) {
            comm_irecv(&req[0],com, buf[0],vsize, id+n/2,id+n/2);
            comm_isend(&req[1],com, red   ,vsize, id+n/2,id);
            comm_wait(req,2);
            gs_gather_array(red,buf[0],vn,dom,op);
        } else {
            comm_irecv(&req[0],com, scan,vsize, base,base);
            comm_isend(&req[1],com, red ,vsize, base,id);
            comm_wait(req,2);
            break;
        }
    }
    while(n>1) {
        if(base==id) {
            comm_send(com, scan  ,2*vsize, id+n/2,id);
        } else {
            comm_recv(com, buffer,2*vsize, base,base);
            gs_gather_array(scan,buf[0],vn,dom,op);
            memcpy(red,buf[1],vsize);
        }
        odd=(odd<<1)|(n&1);
        c<<=1, n>>=1;
        if(id>=base+n) c|=1, base+=n, n+=(odd&1);
    }
}
Exemple #12
0
connection *comm_connect(char *ipaddress, int connections) {
	int i, port;
	connection *conn;
	
	if (in_thread == NULL) start_threads();
	if (connections < 1) return NULL;
	if (ipaddress == NULL) return NULL;
	
	conn = (connection *) malloc(sizeof(connection));
	
	conn->label = strdup(ipaddress);
	conn->connected = 0;
	conn->sockets = (out_socket *) malloc(connections * sizeof(out_socket));
	conn->num_sockets = connections;
	
	// setup the outgoing connections
	for (i = 0; i < connections; i++) {
		conn->sockets[i].fd = connect_to_socket(ipaddress, SOPHIA_PORT);
		set_non_blocking(conn->sockets[i].fd);
		pthread_mutex_init(&(conn->sockets[i].fd_lock), 0);
		conn->sockets[i].backlog = 0;
		conn->sockets[i].done = 0;
	}
	
	conn->connected = 1;
	
	// setup the rget connection
	conn->rget_settings.buffer = cbuf_new();
	conn->rget_settings.client_fd = -1;
	
	conn->rget_settings.server_fd = -1;
	while( conn->rget_settings.server_fd < 0 ) {
		port = randomise_port();
		conn->rget_settings.server_fd = listen_on_socket(port);
	}
	
	// comm_send(conn, 1, "void", 1, NULL, NULL, "[RGetRegisterClient \"%d\" \"%s\"]", port, "10.0.0.12"); // REMOTE get_local_ip());
	comm_send(conn, 1, "void", 1, NULL, NULL, "[RGetRegisterClient \"%d\" \"%s\"]", port, get_local_ip(conn->sockets[0].fd));
	// wait to accept the rget connection
	while (conn->rget_settings.client_fd < 0) { conn->rget_settings.client_fd = accept(conn->rget_settings.server_fd, NULL, 0); }
	
	// add this connect to the inbound subsystem
	comm_in_add(&(conn->rget_settings));
	
	return conn;
}
int oemp_SendCmdOrAck(WORD wDevID, WORD wCmdOrAck, int nParam)
{
	SB_OEM_PKT pkt;
	int nSentBytes;

	pkt.Head1 = (BYTE)STX1;
	pkt.Head2 = (BYTE)STX2;
	pkt.wDevId = wDevID;
	pkt.wCmd = wCmdOrAck;
	pkt.nParam = nParam;
	pkt.wChkSum = oemp_CalcChkSumOfCmdAckPkt(&pkt);

	nSentBytes = comm_send((BYTE*)&pkt, SB_OEM_PKT_SIZE, gCommTimeOut);
	if (nSentBytes != SB_OEM_PKT_SIZE)
		return PKT_COMM_ERR;

	return 0;
}
/*
 * int
 * execute_slaves(slaveset_t *slaveset);
 * -------------------------------------
 *  This function sends the execution command to all the slaves, without
 *  waiting for acknowledgement, causing them to start the execution
 *  immediately.
 *
 *  Mandatory params: slaveset
 *  Optional params :
 *
 *  Return values:
 *   -1 Error
 *    0 Success
 */
int
execute_slaves(slaveset_t *slaveset){
	// Local variables
	slave_t                 *slave = NULL;          // Temporary slave
	int                     err = 0;

	// Iterate through slaves
	slave = slaveset->slave;
	while(slave){
		if (comm_send(slave->slave_fd, MT_SYNEXEC_MSG_EXEC, NULL, NULL, 0) <= 0){
			goto err;
		}
		slave = slave->next;
	}

out:
	// Return
	return(err);

err:
	err = -1;
	goto out;
}
/*
 * static int
 * config_slave(slave_t *slave, char *conf_ptr, off_t conf_len);
 * -------------------------------------------------------------
 *  This function sends the session configuration file to 'slave' and confirms
 *  that he is happy with the contents.
 *
 *  Mandatory params: slave, conf_ptr
 *  Optional params :
 *
 *  Return values:
 *   -1 Error
 *    0 Success
 */
static int
config_slave(slave_t *slave, char *conf_ptr, off_t conf_len){
	// Local variables
	synexec_msg_t		net_msg;		// Synexec msg
	int                     err = 0;                // Return code

	// Send configuration to slave
	if (comm_send(slave->slave_fd, MT_SYNEXEC_MSG_CONF, NULL, conf_ptr, conf_len) < 0){
		goto err;
	}
	if (verbose > 0){
		printf("%s: Configuration sent to slave (%s:%hu).\n", __FUNCTION__, inet_ntoa(slave->slave_addr.sin_addr), ntohs(slave->slave_addr.sin_port));
		fflush(stdout);
	}

	// Await reply
	if (comm_recv(slave->slave_fd, &net_msg, NULL, NULL, NULL) < 0){
		goto err;
	}
	if (net_msg.command != MT_SYNEXEC_MSG_CONF_OK){
		fprintf(stderr, "%s: Slave (%s:%hu) refused configuration file.\n", __FUNCTION__, inet_ntoa(slave->slave_addr.sin_addr), ntohs(slave->slave_addr.sin_port));
		goto err;
	}
	if (verbose > 0){
		printf("%s: Configuration OK from slave (%s:%hu).\n", __FUNCTION__, inet_ntoa(slave->slave_addr.sin_addr), ntohs(slave->slave_addr.sin_port));
		fflush(stdout);
	}

out:
	// Return
	return(err);

err:
	err = -1;
	goto out;
}
Exemple #16
0
static int single_thread_test()
{
    char send_str[] = "send data";
    int send_count = sizeof(send_str);
	int sent = 0;
    char recv_str[10] = {'\0',};
    int rcv_len;
	int received = 0;
	int device = 0;/*which device [0,31]*/
	int times = 1;
    comm_initialize(device,COMM_NBLOCK);

#if 1
/*XXX test create*/
    comm_info("Test create start.\n");
	comm_create(device, nblks, blksz);
    comm_info("Test create end.\n");
#endif

#if 1
/*XXX test send and receive one time.*/
    comm_info("Test send start.\n");
    comm_info("Send data is:%s\n",send_str);
	sent = comm_send(device, send_str, send_count);
    comm_info("Sent length is:%d\n",sent);

    comm_info("Test receive start.\n");
	do
	{
		rcv_len = comm_getlen(device);
	}while(rcv_len < 0);
	comm_receive(device, recv_str, rcv_len);
    comm_info("Received data is:%s\n",recv_str);
    comm_info("Received length is:%d\n",rcv_len);
#endif

#if 1
/*XXX test send and receive many times*/
	while(times<6)
	{
		comm_info("times:%d.\n",times);
		send_str[0]+=1;
		times +=1;
		comm_info("Test send start.\n");
		comm_info("Send data is:%s\n",send_str);
		sent = comm_send(device, send_str, send_count);
		comm_info("Sent length is:%d\n",sent);

		comm_info("Test receive start.\n");
		do
		{
			rcv_len = comm_getlen(device);
		}while(rcv_len < 0);
		comm_receive(device, recv_str, rcv_len);
		comm_info("Received data is:%s\n",recv_str);
		comm_info("Received length is:%d\n",rcv_len);
	}
#endif

    comm_release(device);
	comm_info("Test end.\n");
}
Exemple #17
0
int main(int argc, char **argv) {
	cl_options_t cl_options = {0};

	dbg_init();
	DBG_LOG("Version %s", g_version);

	ui = &ui_sdlgl;

	printf("DreamChess %s\n", g_version);

	parse_options(argc, argv, &ui, &cl_options);
	config_init();
	set_cl_options(&cl_options);

	if (!ui) {
		DBG_ERROR("Failed to find a user interface driver");
		exit(1);
	}

	ui->init();

	init_resolution();

	while (1) {
		board_t board;
		int pgn_slot;
		option_t *option;

		if (!(config = ui->config(&pgn_slot)))
			break;

		ch_userdir();
		option = config_get_option("first_engine");

#ifdef __APPLE__
		char temp1[200];
		char temp2[200];

		if (!strcmp(option->string, "dreamer") || !strcmp(option->string, "Dreamer")) {
			CFBundleRef mainBundle = CFBundleGetMainBundle();

			CFURLRef bundledir = CFBundleCopyBundleURL(mainBundle);
			CFStringRef stringref = CFURLCopyFileSystemPath(bundledir, kCFURLPOSIXPathStyle);
			CFStringGetCString(stringref, temp1, 200, kCFStringEncodingMacRoman);

			snprintf(temp2, sizeof(temp2), "%s/contents/MacOS/dreamer", temp1);

			game_set_engine_error(comm_init(temp2));
		} else
			game_set_engine_error(comm_init(option->string));
#else
		game_set_engine_error(comm_init(option->string));
#endif

		comm_send("xboard\n");

		comm_send("new\n");
		comm_send("random\n");

		comm_send("sd %i\n", config->cpu_level);
		comm_send("depth %i\n", config->cpu_level);

		if (config->difficulty == 0)
			comm_send("noquiesce\n");

		if (config->player[WHITE] == PLAYER_UI && config->player[BLACK] == PLAYER_UI)
			comm_send("force\n");

		if (config->player[WHITE] == PLAYER_ENGINE)
			comm_send("go\n");

		in_game = 1;
		board_setup(&board);
		history = history_init(&board);
		move_list_init(&san_list);
		move_list_init(&fan_list);
		move_list_init(&fullalg_list);

		if (pgn_slot >= 0)
			if (game_load(pgn_slot)) {
				DBG_ERROR("Failed to load savegame in slot %i", pgn_slot);
				exit(1);
			}

		ui->update(history->view->board, NULL);
		while (in_game) {
			char *s;

			if ((s = comm_poll())) {
				DBG_LOG("Message from engine: '%s'", s);
				if (!history->result) {
					if ((!strncmp(s, "move ", 4) || strstr(s, "... ")) &&
						config->player[history->last->board->turn] == PLAYER_ENGINE) {
						char *move_str = strrchr(s, ' ') + 1;
						board_t new_board = *history->last->board;
						move_t *engine_move;

						DBG_LOG("Parsing move string '%s'", move_str);

						engine_move = san_to_move(&new_board, move_str);
						if (!engine_move)
							engine_move = fullalg_to_move(&new_board, move_str);
						if (engine_move) {
							audio_play_sound(AUDIO_MOVE);
							do_move(engine_move, 1);
							free(engine_move);
						} else
							DBG_ERROR("Failed to parse move string '%s'", move_str);
					} else if (strstr(s, "llegal move"))
						game_undo();
					/* Ignore result message if we've already determined a result ourselves. */
					else {
						char *start = strchr(s, '{');
						char *end = strchr(s, '}');

						if (start && end && end > start) {
							char *comment = malloc(end - start);
							history->result = malloc(sizeof(result_t));
							strncpy(comment, start + 1, end - start - 1);
							comment[end - start - 1] = '\0';
							history->result->reason = comment;
							if (strstr(s, "1-0")) {
								history->result->code = RESULT_WHITE_WINS;
								ui->show_result(history->result);
							} else if (strstr(s, "1/2-1/2")) {
								history->result->code = RESULT_DRAW;
								ui->show_result(history->result);
							} else if (strstr(s, "0-1")) {
								history->result->code = RESULT_BLACK_WINS;
								ui->show_result(history->result);
							} else {
								free(history->result->reason);
								free(history->result);
								history->result = NULL;
							}
						}
					}
				}

				free(s);
			}
			ui->poll();
		}
		comm_send("quit\n");
		comm_exit();
		history_exit(history);
		move_list_exit(&san_list);
		move_list_exit(&fan_list);
		move_list_exit(&fullalg_list);
	}
	ui->exit();
	dbg_exit();
	return 0;
}
void
simulation_step(void)
{
  int x, y;
  fish_t fish_src;
  fish_t fish_dest;
  char choice;
  double rand_num;
  int frontier_through = 0;
  int move_success     = 0;
  int dont_act         = 0;
  int dontreplace      = 0;

  column_right = malloc(bande_height*4096);


  struct comm_t comm;
  struct comm_t comm_s;


  struct timeval deb, fin;
  gettimeofday(&deb, NULL);

  clear_stack ();

  /* Si je suis le dernier processeur, je dois garder en mémoire ma dernière colonne */  
  if (myid == numprocs-1)
  {
    for (y = 0; y < bande_height; y++)
      column_right[y] = ocean[(bande_width-1)*ocean_size + y]->type;
  }
  // Je garde en mémoire ma première colonne
  for (y = 0; y < bande_height; y++)
    column_left[y] = ocean[y]->type;

  
  for(y = 0; y < ocean_size; y++) 
  {
    current_line = y;
#ifdef _DISPLAY
    printf("[%d] "BLUE"Simulation ligne n°%d\033[00m\n", myid, y);fflush(stdout);
    
#endif
    // Traite les demandes des voisins 
    request_solve(bande_height+1, 0, &comm_s); 
    for(x = 0; x < bande_width; x++) 
    {
      frontier_through  = 0;
      move_success      = 0;
      dont_act          = 0;
      dontreplace       = 0;

      fish_src = fish_at(x,y);
      
      if (fish_src == NULL)
        continue;
      if (fish_src->type == NOTHING)
        continue;

      choice = give_rnd_case();
      ((int)fish_src->age)++;

      /* Si on tente de traverser la frontière, on doit récupérer fish_dest */
      if (x == 0 && choice == D_WEST)  
      {
#ifdef _DEBUG
        printf("[%d] \033[31mCas 1\033[00m %d\n", myid, column_right[y]);fflush(stdout);
#endif
        
        comm_send(TO_LEFT, x, y, GO_THROUGH); 
        waiting_loop (FROM_LEFT,y,   &comm);
        if (comm.fish.type == NOTHING)
          fish_dest = NULL;
        else
        {
          fish_dest = &comm.fish;
        }
        frontier_through = 1;        
      }
      else if (x == bande_width - 1 && choice == D_EAST)
      {
#ifdef _DEBUG
        printf("[%d] \033[31mCas 2\033[00m %d\n", myid, local_fish_at(x,y)->type);fflush(stdout);
#endif
        comm_send(TO_RIGHT, x, y, GO_THROUGH);  
        waiting_loop(FROM_RIGHT,y, &comm); 
        if (comm.fish.type == NOTHING)
          fish_dest = NULL;
        else
        {
          fish_dest = &comm.fish;
        }
        frontier_through = 1;    
      }
      /* Un poisson veut traverser à gauche */
      else if (x == 0 && fish_src->type == SARDINE)
      {
#ifdef _DEBUG
        printf("[%d] \033[31mCas 3\033[00m\n", myid);fflush(stdout);
#endif
        comm_send(TO_LEFT, x, y, NOT_THROUGH);
        waiting_loop(FROM_LEFT,y,   &comm);
        if (comm.fish.type == NOTHING)
          fish_dest = NULL;
        else
        {
          fish_dest = &comm.fish;
        }
        if (comm.flag == GO_THROUGH && fish_dest->type == SHARK)
        {
          dont_act = 1;
          local_replace_at(x,y,fish_dest);
        }
      }
      /* Cas où le poisson va vers le haut, et que cette case est vide */
      else if (x == bande_width - 1 
               && choice == D_NORTH 
               && y != 0
               && local_fish_at(x,y-1)->type == NOTHING)
      { 
#ifdef _DEBUG
        printf("[%d] \033[31mCas 4\033[00m\n", myid);fflush(stdout);
#endif
        comm_send(TO_RIGHT, x, y-1, NOT_THROUGH);

        waiting_loop(FROM_RIGHT,y-1,   &comm);  
        if (comm.flag == GO_THROUGH)
        {
          fish_dest = &comm.fish;
          dontreplace = 1;
        }
      } 

      /* Cas où le poisson va vers le bas, et que cette case est vide */
      else if (x == 0
               && choice == D_NORTH 
               && y != 0
               && local_fish_at(0,y-1)->type == NOTHING)
      { 
#ifdef _DEBUG
        printf("[%d] \033[31mCas 5\033[00m\n", myid);fflush(stdout);
#endif
        comm_send(TO_LEFT, x, y-1, NOT_THROUGH);
        waiting_loop(FROM_LEFT,y-1,   &comm);  
        if (comm.flag == GO_THROUGH)
        {
          fish_dest = &comm.fish;
          dontreplace = 1;
        }
      }

      /* Cas où le poisson de la 2e case veut aller à gauche */
      else if (x == 1
               && choice == D_WEST 
               && local_fish_at(0,y)->type == NOTHING)
      {  
#ifdef _DEBUG
        printf("[%d] \033[31mCas 6\033[00m ligne %d\n", myid, y);fflush(stdout);
#endif
        comm_send(TO_LEFT, x, y, NOT_THROUGH);
        waiting_loop(FROM_LEFT,y,  &comm);  
        if (comm.flag == GO_THROUGH)
        {
          fish_dest =&comm.fish;
          dontreplace = 1;
        }
      }

      /* Cas où le poisson va vers le bas, et que cette case est une sardine */
      else if (x == bande_width - 1 
               && choice == D_NORTH 
               && y != 0
               && local_fish_at(x,y-1)->type == SARDINE)
      { 
#ifdef _DEBUG
        printf("[%d] \033[31mCas 7\033[00m ligne %d\n", myid, y);fflush(stdout);
#endif
        comm_send(TO_RIGHT, x, y-1, NOT_THROUGH);
        waiting_loop(FROM_RIGHT,y-1,   &comm);  
        if (comm.flag == GO_THROUGH && comm.fish.type == SHARK)
        {
          fish_dest = &comm.fish;
          local_replace_at(x,y-1, fish_dest);
          dontreplace = 1;
        }
      } 

      /* Cas où le poisson va vers le bas, et que cette case est une sardine */
      else if (x == 0
               && choice == D_NORTH 
               && y != 0
               && local_fish_at(0,y-1)->type == SARDINE)
      { 
#ifdef _DEBUG
        printf("[%d] \033[31mCas 8\033[00m ligne %d\n", myid, y);fflush(stdout);
#endif
        comm_send(TO_LEFT, x, y-1, NOT_THROUGH);
        waiting_loop(FROM_LEFT,y-1,   &comm);  
        if (comm.flag == GO_THROUGH && comm.fish.type == SHARK)
        {
          fish_dest = &comm.fish;
          local_replace_at(x,y-1, fish_dest);
          dontreplace = 1;
        }
      }

      /* Cas où le poisson de la 2e case veut aller à gauche */
      else if (x == 1
               && choice == D_WEST 
               && local_fish_at(0,y)->type == SARDINE)
      { 
#ifdef _DEBUG
        printf("[%d] \033[31mCas 9\033[00m ligne %d\n", myid, y);fflush(stdout);
#endif
        comm_send(TO_LEFT, x, y, NOT_THROUGH);
        waiting_loop(FROM_RIGHT,y,   &comm);  
        if (comm.flag == GO_THROUGH && comm.fish.type == SHARK)
        {
          fish_dest = &comm.fish;
          local_replace_at(0, y, fish_dest);
          dontreplace = 1;
        }
      }

      /* RESOLUTION NORMALE */
      switch (fish_src->type)
      {
        case SHARK:
          fish_src->age_before_starvation--;

          /* famine */
          if (!fish_src->age_before_starvation)
            local_kill_at(x,y);

          /* Si on n'a pas dépassé de frontière, on récupère le poisson dest */
          if (frontier_through == 0 && dontreplace == 0)
            fish_dest = local_fish_at(case_move_x(x, y, choice), 
                                      case_move_y(x, y, choice));

          /* S'il n'y a rien, on se déplace */
          if (fish_dest == NOTHING || fish_dest->type == NOTHING)      
          {
#ifdef _DEBUG
              printf("On se déplace\n");fflush(stdout);
#endif
            if (frontier_through == 0)
            {
              local_replace_at(case_move_x(x, y, choice),
                               case_move_y(x, y, choice), fish_src);
              local_erase_at(x, y);
            }
            else
            {
#ifdef _DEBUG
              printf("On efface \n");fflush(stdout);
#endif
              local_kill_at(x, y);
            }

            move_success = 1;

            /* bébé requin? */
            if (fish_src->age >= BREEDING_AGE_SHARK)
            {
              rand_num = ((double)rand())/(RAND_MAX+1.0);  
              if (rand_num <= PROBA_SHARK)
              {
                local_fish_at(x,y)->type = SHARK;
                local_fish_at(x,y)->age = 0;
                local_fish_at(x,y)->age_before_starvation = SHARK;
              }
            }
           
          }
  
          /* Si on tombe sur une sardine */
          else if (fish_dest->type == SARDINE)
          {
            fish_src->age_before_starvation = T_FAMINE;
            if (frontier_through == 0)
            {
              local_replace_at(case_move_x(x, y, choice), 
                               case_move_y(x, y, choice), fish_src);

              local_erase_at(x, y);
#ifdef _DEBUG
              printf("On a bouffé une sardine\n");fflush(stdout);
#endif
            }
            else
              local_kill_at(x, y);   

            move_success = 1;         

            /* bébé requin? */
            if (fish_src->age >= BREEDING_AGE_SHARK)
            {
              rand_num = ((double)rand())/(RAND_MAX+1.0);  
              if (rand_num <= PROBA_SHARK)
              {
                local_fish_at(x,y)->type = SHARK;
                local_fish_at(x,y)->age = 0;
                local_fish_at(x,y)->age_before_starvation = SHARK;
              }
            }
          }

          /* Si on tombe sur un requin... */
          else if (fish_dest->type == SHARK)
          {            
            /* famine */
            if (!fish_src->age_before_starvation)
              local_kill_at(x,y);
          }
                    
          break;

        case SARDINE:
          /* Si on n'a pas dépassé de frontière, on récupère le poisson dest */
          if (frontier_through == 0)
            fish_dest = local_fish_at(case_move_x(x, y, choice), 
                                      case_move_y(x, y, choice));

          /* S'il n'y a rien, on se déplace */
          if (fish_dest == NOTHING || fish_dest->type == NOTHING)      
          {
            if (frontier_through == 0)
            {
              local_replace_at(case_move_x(x, y, choice),
                               case_move_y(x, y, choice), fish_src);
              local_erase_at(x, y);
            }

            local_kill_at(x, y);

            move_success = 1;

            /* bébé sardine? */

            if (fish_src->age >= BREEDING_AGE_SARDINE)
            {
              rand_num = ((double)rand())/(RAND_MAX+1.0);  
              if (rand_num <= PROBA_SARDINE)
              {
                local_fish_at(x,y)->type = SARDINE;
                local_fish_at(x,y)->age = 0;
              }
            }
          }   
  
      }

      /* Actualisation de la première colonne */
      if (x == 0 && y < ocean_size - 1 && choice == D_SOUTH && move_success)
        column_left[y+1] = column_left[y];

      /* Actualisation de la première colonne */
      if (x == bande_width-1 && y < ocean_size && choice == D_SOUTH && move_success)
      {
        column_right[(y+1)%ocean_size] = column_right[y];
      }
      
    }

  }

#ifdef _DISPLAY
  printf("\n\n%d a termine!!\n\n", myid);
  print_bande();
#endif
  current_line++;
  
  gettimeofday(&fin, NULL);

  laps = ((fin.tv_sec*1000000+fin.tv_usec) - (deb.tv_sec*1000000+deb.tv_usec));

  // Synchronisation des montres
  MPI_Request req1, req2;

  comm.flag = TERMINATION;    

  MPI_Isend(&comm, sizeof (struct comm_t), MPI_CHAR, neigh_right(), FROM_LEFT, MPI_COMM_WORLD, &req1);
  MPI_Isend(&comm, sizeof (struct comm_t), MPI_CHAR, neigh_left(), FROM_RIGHT, MPI_COMM_WORLD, &req2);


  while (1)
  {    
    usleep(TIME_TO_WAIT);
    if (left_terminate && right_terminate)
      break;
    request_solve(bande_height+1, 0, &comm_s); 
  }

}