void worker(tdata *args){ MPI_Status status; part_info pi; proc_stat stat, total; int partitions = 0; int done = 0; total.result.strips = 0; total.result.value = 0.0; total.time = 0.0; SENDMSG(&stat, sizeof(stat), M_RANK, MSG_TAG); while(!done/* not all done */){ RECVMSG(&pi, sizeof(pi), status); /* only recv data tags */ if(pi.l_bound < pi.u_bound){ SENDMSG(&stat, sizeof(stat), M_RANK, MSG_TAG); /* do the work */ stat = regproc(args->fun, pi.l_bound, pi.u_bound, args->precision, args->m); partitions++; /* printf("rank %d partition %d trapezoids %d\n", rank, partitions, stat.result.strips); */ total.result.strips += stat.result.strips; total.result.value += stat.result.value; total.time += stat.time; } else { done = 1; } } /* printf("other test %d\n", rank); */ SENDMSG(&total, sizeof(total), M_RANK, DATA_TAG); }
/* * manager worker scheme */ void manager(tdata *args){ MPI_Status status; pthread_t generator; proc_stat stat; part_info *ppi; part_info pi; double total_time = 0.0, final_result = 0.0, avg_trape = 0.0; int rc, flag = 0, max_trape = 0, min_trape = 0, worker_rank; q_part = lqopen(); /* start a thread to do partition */ rc = pthread_create(&generator, NULL, generate, args); pthread_join(generator, NULL); while(flag != size - 1/* workers not all done */){ RECVMSG(&stat, sizeof(proc_stat), status); if(status.MPI_TAG == MSG_TAG){ if(!generator_flag || NULL != lqget(q_part)){ ppi = (part_info *)lqgetrm(q_part); if(NULL != ppi){ SENDMSG(ppi, sizeof(part_info), status.MPI_SOURCE, DATA_TAG); } } else { pi.l_bound = 1.0; pi.u_bound = 0.0; SENDMSG(&pi, sizeof(part_info), status.MPI_SOURCE, DATA_TAG); } } else {/* must be data tag */ worker_rank = status.MPI_SOURCE; min_trape = ((min_trape > stat.result.strips || flag == 0)?stat.result.strips:min_trape); max_trape = ((max_trape < stat.result.strips || flag == 0)?stat.result.strips:max_trape); avg_trape += stat.result.strips; final_result += stat.result.value; total_time += stat.time; flag++; /* printf("test %d\n", flag); */ } } avg_trape /= size; printf("avg trape: %f, max trape: %d, min trape: %d, result: %f, total time: %f\n", avg_trape, max_trape, min_trape, final_result, total_time); lqclose(q_part); }
int acl_read_fd(int fd, void *ptr, int nbytes, int *recv_fd) { struct msghdr msg; struct iovec iov[1]; int n; #if defined(HAVE_MSGHDR_MSG_CONTROL) && !defined(MINGW) const char *myname = "acl_read_fd"; union { struct cmsghdr cm; # ifdef ACL_MACOSX char control[1024]; # else char control[CMSG_SPACE(sizeof(int))]; # endif } control_un; struct cmsghdr *cmptr; int *fdptr; msg.msg_control = control_un.control; msg.msg_controllen = sizeof(control_un.control); #else int newfd; msg.msg_accrights = (caddr_t) &newfd; msg.msg_accrightslen = sizeof(int); #endif msg.msg_name = NULL; msg.msg_namelen = 0; iov[0].iov_base = ptr; iov[0].iov_len = nbytes; msg.msg_iov = iov; msg.msg_iovlen = 1; if ((n = RECVMSG(fd, &msg, 0)) <= 0) return n; #if defined(HAVE_MSGHDR_MSG_CONTROL) && !defined(MINGW) if ((cmptr = CMSG_FIRSTHDR(&msg)) != NULL && cmptr->cmsg_len == CMSG_LEN(sizeof(int))) { if (cmptr->cmsg_level != SOL_SOCKET) acl_msg_fatal("%s: control level != SOL_SOCKET", myname); if (cmptr->cmsg_type != SCM_RIGHTS) acl_msg_fatal("%s: control type != SCM_RIGHTS", myname); #if 0 *recv_fd = *((int *) CMSG_DATA(cmptr)); #else fdptr = (int *) CMSG_DATA(cmptr); *recv_fd = *fdptr; #endif } else *recv_fd = -1; /* descriptor was not passed */ #else if (msg.msg_accrightslen == sizeof(int)) *recv_fd = newfd; else *recv_fd = -1; /* descriptor was not passed */ #endif return n; }