Skip to content
com_mpi.c 15 KiB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
#include "includes.h"
#include <mpi.h>
//#define COM_BIT 0x40000000 //now defined in macros.h

int global_com_bit=COM_BIT;

/* Global variables */
int totnodes[4];		/* number of nodes in machine directions */
int Mynode[4], node_parity;
int **neighbor;
int offnode_even[8];		/* # of even sites that have off-node neighbors in a dir */
int offnode_odd[8];		/* # of odd sites that have off-node neighbors in a dir */
MPI_Comm comm_grid, comm_subgrid[4]; /* grid communicators */

/* print on 0 node only */
void node0_printf( const char *fmt, ... )
{
    va_list argp;
    if( this_node == 0 )
    {
	va_start( argp, fmt );
	vprintf( fmt, argp );
	va_end( argp );
    }

    fflush( 0 );

    MPI_Barrier( MPI_COMM_WORLD );
}

void node0_fprintf( FILE * file, const char *fmt, ... )
{
    va_list argp;
    if( this_node == 0 )
    {
	va_start( argp, fmt );
	vfprintf( file, fmt, argp );
	va_end( argp );
    }

    fflush( 0 );

    MPI_Barrier( MPI_COMM_WORLD );
}

void verbose_fprintf( FILE * file, const char *fmt, ... )
{
    va_list argp;
    if( verbose && this_node == 0 )
    {
	va_start( argp, fmt );
	vfprintf( file, fmt, argp );
	va_end( argp );
    }

    fflush( 0 );

    MPI_Barrier( MPI_COMM_WORLD );
}

/* JuBE  no args needed */
void initialize_machine_KE()
{
    int free_coords[4], wrap_around[4];
    
    MPI_Comm_size(MPI_COMM_WORLD, &number_of_nodes);

    MPI_Comm_rank(MPI_COMM_WORLD, &this_node);

    /* get the totnodes[dir] from parameters file */
    FILE *fpar;
/*     JuBE  set para file to kernel_E.input*/
    if( ( fpar = fopen( "kernel_E.input", "r" ) ) == 0 && this_node == 0 )
    {
        printf( "ERROR initialize_machine: missing parameter file\n" );
        fflush(0);
        exit( 1 );
    }
    if (get_totnodes( fpar, "totnodes" ) && this_node==0)
    {
        printf( "ERROR initialize_machine: missing totnodes\n" );     
        fflush(0);
        exit(1);
    }
  
    if (totnodes[XUP]*totnodes[YUP]*totnodes[ZUP]*totnodes[TUP]!=number_of_nodes &&
            this_node==0)
    {
        printf( "ERROR initialize_machine: bad total number of nodes\n" );     
        fflush(0);
        exit(1);
    }
    fclose(fpar);
    
    /* Cartesian grid */
    wrap_around[0] = 1; 
    wrap_around[1] = 1; 
    wrap_around[2] = 1; 
    wrap_around[3] = 1; 
    MPI_Cart_create(MPI_COMM_WORLD, 4, totnodes, wrap_around, 1, &comm_grid);
            
    /* new coordinates */
    MPI_Comm_rank(comm_grid, &this_node);
    MPI_Cart_coords(comm_grid, this_node, 4,Mynode);

    node_parity = ( Mynode[XUP] + Mynode[YUP] + Mynode[ZUP] + Mynode[TUP] ) % 2;
    node0_printf( "initialize_machine: topology: %dx%dx%dx%d, mynode: %d,%d,%d,%d, numnodes: %d\n",
            totnodes[XUP], totnodes[YUP], totnodes[ZUP], totnodes[TUP],
            Mynode[XUP], Mynode[YUP], Mynode[ZUP], Mynode[TUP], numnodes_KE(  ) );

    /* set up communicators */
    free_coords[XUP] = 1;
    free_coords[YUP] = 0;
    free_coords[ZUP] = 0;
    free_coords[TUP] = 0;
    MPI_Cart_sub(comm_grid, free_coords,&comm_subgrid[XUP]);
    free_coords[XUP] = 0;
    free_coords[YUP] = 1;
    free_coords[ZUP] = 0;
    free_coords[TUP] = 0;
    MPI_Cart_sub(comm_grid, free_coords,&comm_subgrid[YUP]);
    free_coords[XUP] = 0;
    free_coords[YUP] = 0;
    free_coords[ZUP] = 1;
    free_coords[TUP] = 0;
    MPI_Cart_sub(comm_grid, free_coords,&comm_subgrid[ZUP]);
    free_coords[XUP] = 0;
    free_coords[YUP] = 0;
    free_coords[ZUP] = 0;
    free_coords[TUP] = 1;
    MPI_Cart_sub(comm_grid, free_coords,&comm_subgrid[TUP]);

}

static char name[] = "Generic communication";
char *machine_type_KE(  )
{
    return ( name );
}

int mynode_KE(  )
{
    return Mynode[TUP]+totnodes[TUP]*Mynode[ZUP]+
        totnodes[TUP]*totnodes[ZUP]*Mynode[YUP]+
        totnodes[TUP]*totnodes[ZUP]*totnodes[YUP]*Mynode[XUP];
}

void mynode4( int *n_x, int *n_y, int *n_z, int *n_t )
{
    *n_x = Mynode[XUP];
    *n_y = Mynode[YUP];
    *n_z = Mynode[ZUP];
    *n_t = Mynode[TUP];
}

int numnodes_KE(  )
{
    return ( totnodes[XUP] * totnodes[YUP] * totnodes[ZUP] * totnodes[TUP] );
}

void numnodes4( int *n_x, int *n_y, int *n_z, int *n_t )
{
    *n_x = totnodes[XUP];
    *n_y = totnodes[YUP];
    *n_z = totnodes[ZUP];
    *n_t = totnodes[TUP];
}

void gen_send_recv( int dir, char *sbuf, char *rbuf, int size )
{
    MPI_Status status;
    int source;
    int dest;

    if (dir<0 || dir>7)
    {
        node0_fprintf(file_o1, "ERROR gen_send_recv: Bad direction %d\n",dir);
        exit(1);
    }

    if (dir<4)
    {
        /* positive direction */
        source=(Mynode[dir]+1)%totnodes[dir];
        dest=(Mynode[dir]-1+totnodes[dir])%totnodes[dir];
    }
    else 
    {
        /* negative direction */
        dir=OPP_DIR(dir); 
        dest=(Mynode[dir]+1)%totnodes[dir];
        source=(Mynode[dir]-1+totnodes[dir])%totnodes[dir];
    }
      
    MPI_Sendrecv(sbuf,size,MPI_BYTE,dest,0,
            rbuf,size,MPI_BYTE,source,0,
            comm_subgrid[dir],&status);
}

void make_nn_gathers(  )
{
    int x, y, z, t, xp, yp, zp, tp, xm, ym, zm, tm;
    int i, ixp, ixm, iyp, iym, izp, izm, itp, itm, p;
    MEMALIGN(neighbor, int *, 8 );
    for ( i = 0; i < 8; i++ )
        MEMALIGN(neighbor[i],int, sites_on_node);
    /* neighbor = malloc( 8 * sizeof( int * ) ); */
    /* for ( i = 0; i < 8; i++ ) */
    /* { */
	/* neighbor[i] = malloc( sites_on_node * sizeof( int ) ); */
    /* } */
    for ( i = 0; i < 8; i++ )
    {
	offnode_even[i] = offnode_odd[i] = 0;
    }
    for ( x = 0; x < nx; x++ )
	for ( y = 0; y < ny; y++ )
	    for ( z = 0; z < nz; z++ )
		for ( t = 0; t < nt; t++ )
		    if( node_number_KE( x, y, z, t ) == mynode_KE(  ) )
		    {
			i = node_index_KE( x, y, z, t );
			p = lattice[i].parity;
			xp = ( x + 1 ) % nx;
			xm = ( x - 1 + nx ) % nx;
			yp = ( y + 1 ) % ny;
			ym = ( y - 1 + ny ) % ny;
			zp = ( z + 1 ) % nz;
			zm = ( z - 1 + nz ) % nz;
			tp = ( t + 1 ) % nt;
			tm = ( t - 1 + nt ) % nt;
			ixp = node_index_KE( xp, y, z, t );
			ixm = node_index_KE( xm, y, z, t );
			iyp = node_index_KE( x, yp, z, t );
			iym = node_index_KE( x, ym, z, t );
			izp = node_index_KE( x, y, zp, t );
			izm = node_index_KE( x, y, zm, t );
			itp = node_index_KE( x, y, z, tp );
			itm = node_index_KE( x, y, z, tm );

			if( node_number_KE( xp, y, z, t ) == mynode_KE(  ) )
			{
			    neighbor[XUP][i] = ixp;
			}
			else
			{
			    neighbor[XUP][i] = ixp + COM_BIT;
			    if( p == EVEN )
			    {
				offnode_even[XUP]++;
			    }
			    else
				offnode_odd[XUP]++;
			}

			if( node_number_KE( xm, y, z, t ) == mynode_KE(  ) )
			{
			    neighbor[XDOWN][i] = ixm;
			}
			else
			{
			    neighbor[XDOWN][i] = ixm + COM_BIT;
			    if( p == EVEN )
			    {
				offnode_even[XDOWN]++;
			    }
			    else
				offnode_odd[XDOWN]++;
			}
			if( node_number_KE( x, yp, z, t ) == mynode_KE(  ) )
			{
			    neighbor[YUP][i] = iyp;
			}
			else
			{
			    neighbor[YUP][i] = iyp + COM_BIT;
			    if( p == EVEN )
			    {
				offnode_even[YUP]++;
			    }
			    else
				offnode_odd[YUP]++;
			}
			if( node_number_KE( x, ym, z, t ) == mynode_KE(  ) )
			{
			    neighbor[YDOWN][i] = iym;
			}
			else
			{
			    neighbor[YDOWN][i] = iym + COM_BIT;
			    if( p == EVEN )
			    {
				offnode_even[YDOWN]++;
			    }
			    else
				offnode_odd[YDOWN]++;
			}
			if( node_number_KE( x, y, zp, t ) == mynode_KE(  ) )
			{
			    neighbor[ZUP][i] = izp;
			}
			else
			{
			    neighbor[ZUP][i] = izp + COM_BIT;
			    if( p == EVEN )
			    {
				offnode_even[ZUP]++;
			    }
			    else
				offnode_odd[ZUP]++;
			}
			if( node_number_KE( x, y, zm, t ) == mynode_KE(  ) )
			{
			    neighbor[ZDOWN][i] = izm;
			}
			else
			{
			    neighbor[ZDOWN][i] = izm + COM_BIT;
			    if( p == EVEN )
			    {
				offnode_even[ZDOWN]++;
			    }
			    else
				offnode_odd[ZDOWN]++;
			}
			if( node_number_KE( x, y, z, tp ) == mynode_KE(  ) )
			{
			    neighbor[TUP][i] = itp;
			}
			else
			{
			    neighbor[TUP][i] = itp + COM_BIT;
			    if( p == EVEN )
			    {
				offnode_even[TUP]++;
			    }
			    else
				offnode_odd[TUP]++;
			}
			if( node_number_KE( x, y, z, tm ) == mynode_KE(  ) )
			{
			    neighbor[TDOWN][i] = itm;
			}
			else
			{
			    neighbor[TDOWN][i] = itm + COM_BIT;
			    if( p == EVEN )
			    {
				offnode_even[TDOWN]++;
			    }
			    else
				offnode_odd[TDOWN]++;
			}
		    }
}				/*}}} */

msg_tag *start_gather_from_temp(
				    /* arguments */
				    void *field,	/* which field? Pointer to the field array */
				    int size,	/* size in bytes of the field */
				    int dist,	/* distance of elements in the array */
				    int dir,	/* direction to gather from. eg XUP - index into
						   neighbor tables */
				    int parity,	/* parity of sites whose neighbors we gather.
						   one of EVEN, ODD or EVENANDODD. */
				    char **dest )	/* one of the vectors of pointers */
{
    int i, n, in, n_off;
    site *st;
    msg_tag *mbuf;

    mbuf = 0;

    switch ( parity )
    {
	case EVEN:
	    n_off = offnode_even[dir];
	    break;
	case ODD:
	    n_off = offnode_odd[dir];
	    break;
	case EVENANDODD:
	    n_off = offnode_even[dir] + offnode_odd[dir];
	    break;
	default:
	    printf( "ERROR start_gather_from_temp: Wrong parity\n" );
	    return ( 0 );
    }

    //printf("n_off=%d\n",n_off);

    if( n_off > 0 )
    {				// Communication is needed 
        MEMALIGN(mbuf, msg_tag, 1 );
	/* mbuf = ( msg_tag * ) malloc( sizeof( msg_tag ) ); */
	mbuf->size = size;
        MEMALIGN(mbuf->rbuf, char, size*n_off);
        MEMALIGN(mbuf->sbuf, char, size*n_off);
        MEMALIGN(mbuf->ptr, char *, n_off );
	/* mbuf->rbuf = memalign( 16, size * n_off ); */
	/* mbuf->sbuf = memalign( 16, size * n_off ); */
	/* mbuf->ptr = malloc( sizeof( char * ) * n_off ); */
	mbuf->done = 0;
	mbuf->n = n_off;
    }
    n = 0;

    //printf("dir=%d,ngb=%d \n", dir,neighbor[dir][0]); 
  
    FORSOMEPARITY( i, st, parity )
    {
      //AG: off node neighbours labeled by an integer greater than COM_BIT (idx + COM_BIT)
	if( ( in = neighbor[dir][i] ) < COM_BIT )
	{

	  //AG: point dest[i] to multi-valued site associated with (on-node) neighbour
	    dest[i] = ( char * ) field + dist * in;
	}
	else
	{

	  //AG: point dest[i] to multi-valued site associated with (off-node) neighbour
	  //AG: which exists in recieve buffer
	    dest[i] = mbuf->rbuf + n * size;
	    //AG prepare to create send buffer. Set pointer to opposite value to be sent other way
	    mbuf->ptr[n] = ( char * ) field + dist * ( in - COM_BIT );
	    //AG: and copy multi-valued data to send buffer
	    memcpy( mbuf->sbuf + n * size, mbuf->ptr[n], size );
	    n++;
	}
    }

    /* node0_fprintf(stderr,"No of off points %d\n",n_off); */

    if( n_off > 0 )
    {
	if( dir > 7 )
	    dir -= 8;
	gen_send_recv( dir,  mbuf->sbuf,  mbuf->rbuf , ( mbuf->size ) * ( mbuf->n )  );
    }
    return ( mbuf );
}

void restart_gather_from_temp(
				  /* arguments */
				  void *field,	/* which field? Some member of structure "site" */
				  int size,	/* size in bytes of the field */
				  int dist,	/* distance of elements in the array */
				  int dir,	/* direction to gather from. eg XUP - index into
						   neighbor tables */
				  int parity,	/* parity of sites whose neighbors we gather.
						   one of EVEN, ODD or EVENANDODD. */
				  char **dest,	/* one of the vectors of pointers */
				  msg_tag * mbuf )	/* previously returned by start_gather */
{
    int i;
    if( mbuf == 0 )
	return;
    if( mbuf->done != 1 )
    {
	node0_printf( "ERROR restart_gather:previos gather was not waited for\n" );
	return;
    }
    mbuf->done = 0;
    for ( i = 0; i < mbuf->n; i++ )
    {
	memcpy( mbuf->sbuf + i * ( mbuf->size ), mbuf->ptr[i], mbuf->size );
    }
    if( dir > 7 )
	dir -= 8;
    gen_send_recv( dir,  mbuf->sbuf , mbuf->rbuf , ( mbuf->size ) * ( mbuf->n )  );
}

msg_tag *start_general_gather_from_temp(
					    /* arguments */
					    void *field,	/* which field? Pointer to the field array */
					    int size,	/* size in bytes of the field (eg sizeof(su3_vector)) */
					    int dist,	/* distance of elements in the array */
					    int *dst,	/* direction to gather from. eg XUP - index into
							   neighbor tables */
					    int parity,	/* parity of sites whose neighbors we gather.
							   one of EVEN, ODD or EVENANDODD. */
					    char **dest )	/* one of the vectors of pointers */
{
    char *buf2;
    char **ptr;
    int i, d, dir, nd, n;
    site *st;
    msg_tag *tg;
    msg_tag *mbuf;

    MEMALIGN(mbuf, msg_tag, 1) ;
    mbuf->size=size;
    mbuf->n=sites_on_node;
    MEMALIGN(mbuf->rbuf, char, size*sites_on_node);
    /* mbuf = ( msg_tag * ) malloc( sizeof( msg_tag ) ); */
    /* mbuf->rbuf = memalign( 16, size * sites_on_node ); */

    MEMALIGN(ptr, char *, sites_on_node);
    MEMALIGN(buf2, char, size*sites_on_node);
    /* ptr = malloc( sites_on_node * sizeof( char * ) ); */
    /* buf2 = memalign( 16, size * sites_on_node ); */

    FORALLSITES( i, st )
    {
	memcpy( mbuf->rbuf + i * size, ( char * ) field + i * dist, size );
    }
    for ( d = 0; d < 4; d++ )
    {
	dir = d;
	nd = dst[d];

	if( nd < 0 )
	{
	    dir = 7 - d;
	    nd = -nd;
	}
	for ( n = 0; n < nd; n++ )
	{
	    tg = start_gather_from_temp( mbuf->rbuf, size, size, dir, EVENANDODD, ptr );
	    wait_gather_KE( tg );
	    // copy to buf2 
	    FORALLSITES( i, st )
	    {
		memcpy( buf2 + i * size, ptr[i], size );
	    }
	    // copy back to buf1 
	    memcpy( mbuf->rbuf, buf2, size * sites_on_node );
	    cleanup_gather( tg );
	}
    }
    FORSOMEPARITY( i, st, parity )
    {
	dest[i] = mbuf->rbuf + i * size;
    }

    FREE(ptr, char *, sites_on_node);
    FREE(buf2, char, size*sites_on_node);

    return mbuf;
}

void wait_gather_KE( msg_tag * mbuf )
{
    if( mbuf == 0 )
	return;
    mbuf->done = 1;
    return;
}

void wait_general_gather( msg_tag * mbuf )
{
}

void cleanup_gather( msg_tag * mbuf )
{
    if( mbuf != 0 )
    {
        FREE(mbuf->rbuf, char, (mbuf->size)*(mbuf->n));
        FREE(mbuf->sbuf, char, (mbuf->size)*(mbuf->n));
        FREE(mbuf->ptr, char *, mbuf->n );
        FREE(mbuf, msg_tag, 1 );
    }
}

void cleanup_general_gather( msg_tag * mbuf )
{
    if( mbuf != 0 )
    {
        FREE(mbuf->rbuf, char, (mbuf->size)*(mbuf->n));
        FREE(mbuf, msg_tag, 1 );
    }
}

/* Synchronize all nodes by sending one double to each directions */
void g_sync_KE(  )
{
    MPI_Barrier( MPI_COMM_WORLD );
}

void g_doublesum_KE( double *dpt )
{
    double work;
    MPI_Allreduce( dpt, &work, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD );
    *dpt = work;
}

void g_doublemax_KE( double *dpt )
{
    double work;
    MPI_Allreduce( dpt, &work, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );
    *dpt = work;
}

void broadcast_double_KE( double *dpt )
{
    MPI_Bcast( dpt, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD );
}

void broadcast_char( char *buf, int size )
{
    MPI_Bcast( buf, size, MPI_BYTE, 0, MPI_COMM_WORLD );
}

void broadcast_int_KE( int *buf )
{
    MPI_Bcast( buf, 1, MPI_INTEGER, 0, MPI_COMM_WORLD );
}

/* Double precision time */
double dclock(  )
{
    return MPI_Wtime();
}

/* version of exit for multinode processes -- kill all nodes */
void terminate_KE( int status )
{
    printf( "terminate: node %d, status = %d\n", this_node, status );
    exit( status );
}