コード例 #1
0
ファイル: barrier.c プロジェクト: Shurakai/SimGrid
int
main( int argc, char **argv)
{
    int rank, size, i, recv_flag, ret, passed;
    MPI_Status Status;
    char message[17];
    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);

    if (rank == 0) {
	Test_Init("barrier", rank);
	/* Receive the startup messages from each of the 
	   other clients */
	for (i = 0; i < size - 1; i++) {
	    MPI_Recv(message, 17, MPI_CHAR, MPI_ANY_SOURCE, 2000, 
		     MPI_COMM_WORLD, &Status);
	}

	/* Now use Iprobe to make sure no more messages arive for a
	    while */
	passed = 1;
	for (i = 0; i < WAIT_TIMES; i++){
	    recv_flag = 0;
	    MPI_Iprobe(MPI_ANY_SOURCE, 2000, MPI_COMM_WORLD, 
		       &recv_flag, &Status);
	    if (recv_flag)
		passed = 0;
	}

	if (passed)
	    Test_Passed("Barrier Test 1");
	else
	    Test_Failed("Barrier Test 1");

	/* Now go into the barrier myself */
	MPI_Barrier(MPI_COMM_WORLD);

	/* And get everyones message who came out */
	for (i = 0; i < size - 1; i++) {
	    MPI_Recv(message, 13, MPI_CHAR, MPI_ANY_SOURCE, 2000, 
		     MPI_COMM_WORLD, &Status);
	}

	/* Now use Iprobe to make sure no more messages arive for a
	    while */
	passed = 1;
	for (i = 0; i < WAIT_TIMES; i++){
	    recv_flag = 0;
	    MPI_Iprobe(MPI_ANY_SOURCE, 2000, MPI_COMM_WORLD, 
		       &recv_flag, &Status);
	    if (recv_flag)
		passed = 0;
	}
	if (passed)
	    Test_Passed("Barrier Test 2");
	else
	    Test_Failed("Barrier Test 2");

	Test_Waitforall( );
	ret = Summarize_Test_Results();
	Test_Finalize();
	MPI_Finalize();
	return ret;
    } else {
	MPI_Send((char*)"Entering Barrier", 17, MPI_CHAR, 0, 2000, MPI_COMM_WORLD);
	MPI_Barrier(MPI_COMM_WORLD);
	MPI_Send((char*)"Past Barrier", 13, MPI_CHAR, 0, 2000, MPI_COMM_WORLD);
	Test_Waitforall( );
	MPI_Finalize();
	return 0;
    }
}
コード例 #2
0
ファイル: bcastvec.c プロジェクト: luyukunphy/namd
int main( int argc, char **argv )
{
    int rank, size, ret, passed, i, *test_array;
    int stride, count, root;
    MPI_Datatype newtype;
    MPI_Comm     comm = MPI_COMM_WORLD;

    /* Set up MPI */
    MPI_Init(&argc, &argv);
    MPI_Comm_rank(comm, &rank);

    /* Setup the tests */
    Test_Init("bcastvec", rank);

    /* Allow for additional communicators */
    MPI_Comm_size(comm, &size);
    /* MPI_Comm_rank(comm, &rank); */
    stride = (rank + 1);
    test_array = (int *)malloc(size*stride*sizeof(int));

    /* Create the vector datatype EXCEPT for process 0 (vector of
       stride 1 is contiguous) */
    if (rank > 0) {
        count = 1;
        MPI_Type_vector( size, 1, stride, MPI_INT, &newtype);
        MPI_Type_commit( &newtype );
    }
    else {
        count = size;
        newtype = MPI_INT;
    }

    /* Perform the test.  Each process in turn becomes the root.
       After each operation, check that nothing has gone wrong */
    passed = 1;
    for (root = 0; root < size; root++) {
        /* Fill the array with -1 for unset, rank + i * size for set */
        for (i=0; i<size*stride; i++) test_array[i] = -1;
        if (rank == root)
            for (i=0; i<size; i++) test_array[i*stride] = rank + i * size;
        MPI_Bcast( test_array, count, newtype, root, comm );
        for (i=0; i<size; i++) {
            if (test_array[i*stride] != root + i * size) {
                passed = 0;
            }
        }
    }
    free(test_array);
    if (rank != 0) MPI_Type_free( &newtype );

    if (!passed)
        Test_Failed("Simple Broadcast test with datatypes");
    else {
        if (rank == 0)
            Test_Passed("Simple Broadcast test with datatypes");
    }

    /* Close down the tests */
    if (rank == 0)
        ret = Summarize_Test_Results();
    else {
        ret = 0;
    }
    Test_Finalize();

    /* Close down MPI */
    Test_Waitforall( );
    MPI_Finalize();
    return ret;
}