/* this example was meant to be brief, so we've omitted checks for * Message Board routines' return code. * In practice, you would want to check all return code, and handle any * erroneous conditions */ int main(int argc, char **argv) { int rc; void *msg; int id, pcount; MBt_Board mboard; MBt_Iterator iter; double mypi, pi, sum, h; /* Initialise Message Board environment */ MB_Env_Init(); /* get my ID and proc count */ id = MB_Env_GetProcID(); pcount = MB_Env_GetProcCount(); /* Create a board that can store doubles */ MB_Create(&mboard, sizeof(double)); /* calculate my portion of pi */ h = 1.0 / (double)NR; pi = sum = 0.0; for (i = id + 1; i <= NR; i += pcount) { sum += FUNC_X(((double)i - 0.5)*h); } /* Everyone adds their sum to the board */ MB_AddMessage(mboard, &sum); /* Synchornise boards */ /* If there are more things to do that does not involved the board * we can run them between SyncStart() and SyncComplete(). That will * overlap the communication time with your own computation. */ MB_SyncStart(mboard); MB_SyncComplete(mboard); /* Master node reads message and prints out result */ if (id == 0) { MB_Iterator_Create(mboard, &iter); MB_Iterator_GetMessage(iter, msg); while (msg) { pi += *((double *)msg); MB_Iterator_GetMessage(iter, msg); } MB_Iterator_Delete(&iter); printf("Pi is approximately %.16f\n", pi); } /* Delete board */ MB_Delete(&mboard); /* Finalise Message Board environment */ return MB_Env_Finalise(); }
/* * Looks for the minimum between two 32-bit image pixels (a central pixel * and its neighbors in the other image). * The neighbor depends on the grid used (see mambaCommon.h). Neighbors are * described using a pattern (see enum MB_Neighbors_code_t). If no neighbor * is defined, the function will leave silently doing nothing. * * \param src source image in which the neighbor are taken * \param srcdest source of the central pixel and destination image * \param neighbors the neighbors to take into account * \param grid the grid used (either square or hexagonal) * \param edge the kind of edge to use (behavior for pixel near edge depends on it) * * \return An error code (MB_NO_ERR if successful) */ MB_errcode MB_InfNb32(MB_Image *src, MB_Image *srcdest, Uint32 neighbors, enum MB_grid_t grid, enum MB_edgemode_t edge) { Uint32 bytes_in; MB_Image *temp; PLINE *plines_in, *plines_inout; MB_errcode err; PIX32 edge_val = I32_FILL_VALUE(edge); /* Error management */ /* Verification over image size compatibility */ if (!MB_CHECK_SIZE_2(src, srcdest)) { return MB_ERR_BAD_SIZE; } /* Only greyscale images can be processed */ switch (MB_PROBE_PAIR(src, srcdest)) { case MB_PAIR_32_32: break; default: return MB_ERR_BAD_DEPTH; } /* If src and srcdest are the same image, we create a temporary */ /* images where we copy the pixels value */ if (src==srcdest) { temp = MB_malloc(sizeof(MB_Image)); if (temp==NULL) { return MB_ERR_CANT_ALLOCATE_MEMORY; } err = MB_Create(temp, src->width, src->height, src->depth); if (err!=MB_NO_ERR) { return err; } err = MB_Copy(src, temp); if (err!=MB_NO_ERR) { return err; } } else { temp = src; } /* Setting up pointers */ plines_in = temp->plines; plines_inout = srcdest->plines; bytes_in = MB_LINE_COUNT(src); /* Calling the corresponding function */ if (grid==MB_SQUARE_GRID) { if ((neighbors&MB_NEIGHBOR_ALL_SQUARE)==0) { /* No neighbors to take into account */ return MB_NO_ERR; } MB_comp_neighbors_square(plines_inout, plines_in, bytes_in, temp->height, neighbors, edge_val); } else { if ((neighbors&MB_NEIGHBOR_ALL_HEXAGONAL)==0) { /* No neighbors to take into account */ return MB_NO_ERR; } MB_comp_neighbors_hexagonal(plines_inout, plines_in, bytes_in, temp->height, neighbors, edge_val); } /* Destroying the temporary image if one was created */ if (src==srcdest) { MB_Destroy(temp); } return MB_NO_ERR; }
/* Test MB_Iterator_Rewind */ void test_mb_s_iter_rewind(void) { int rc, checkval; MBt_Board mb; MBt_Iterator itr; MBIt_Iterator *iter_obj; void *obj; /* testing invalid iterator */ itr = 999999; rc = MB_Iterator_Rewind(itr); CU_ASSERT_EQUAL(rc, MB_ERR_INVALID); /* testing null iterator */ itr = MB_NULL_ITERATOR; rc = MB_Iterator_Rewind(itr); CU_ASSERT_EQUAL(rc, MB_ERR_INVALID); /* Create empty iterator/board */ rc = MB_Create(&mb, sizeof(dummy_msg)); CU_ASSERT_EQUAL_FATAL(rc, MB_SUCCESS); rc = MB_Iterator_Create(mb, &itr); CU_ASSERT_EQUAL_FATAL(rc, MB_SUCCESS); /* rewind empty iterator */ rc = MB_Iterator_Rewind(itr); CU_ASSERT_EQUAL(rc, MB_SUCCESS); /* check internal values */ iter_obj = (MBIt_Iterator *)MBI_getIteratorRef(itr); CU_ASSERT_PTR_NOT_NULL_FATAL(iter_obj); CU_ASSERT_EQUAL(iter_obj->iterating, 0); CU_ASSERT_PTR_NULL(iter_obj->cursor); /* clean up */ iter_obj = NULL; MB_Iterator_Delete(&itr); MB_Delete(&mb); /* create and populate board */ rc = init_mb_with_content(&mb); CU_ASSERT_EQUAL_FATAL(rc, MB_SUCCESS); /* create iterator */ rc = MB_Iterator_Create(mb, &itr); CU_ASSERT_EQUAL_FATAL(rc, MB_SUCCESS); /* check internal values (before starting iteration) */ iter_obj = (MBIt_Iterator *)MBI_getIteratorRef(itr); CU_ASSERT_PTR_NOT_NULL_FATAL(iter_obj); CU_ASSERT_EQUAL(iter_obj->iterating, 0); CU_ASSERT_PTR_NULL(iter_obj->cursor); /* get first message */ rc = MB_Iterator_GetMessage(itr, &obj); CU_ASSERT_EQUAL_FATAL(rc, MB_SUCCESS); CU_ASSERT_EQUAL(iter_obj->iterating, 1); CU_ASSERT_PTR_NOT_NULL(iter_obj->cursor); checkval = ((dummy_msg *)obj)->ernet; while(obj) { /* iterate till the end */ free(obj); /* release mem */ MB_Iterator_GetMessage(itr, &obj); } /* rewind and check content */ rc = MB_Iterator_Rewind(itr); CU_ASSERT_EQUAL(rc, MB_SUCCESS); CU_ASSERT_EQUAL(iter_obj->iterating, 0); CU_ASSERT_PTR_NULL(iter_obj->cursor); /* get another object and compare with obj_first */ rc = MB_Iterator_GetMessage(itr, &obj); CU_ASSERT_EQUAL_FATAL(rc, MB_SUCCESS); CU_ASSERT_EQUAL(checkval, ((dummy_msg *)obj)->ernet); /* clean up */ free(obj); MB_Iterator_Delete(&itr); MB_Delete(&mb); }