void dosend(int dest, rndtype val, rndtype finish, MPI_Request* recvReq, rndtype recvBuf[]) { MPI_Request sendReq; int sendFlag, recvFlag; MPI_Status sendStat, recvStat; rndtype buf[2]; buf[0] = val; buf[1] = finish; MPI_Issend(buf, 2*sizeof(rndtype), MPI_BYTE, dest, 1, MPI_COMM_WORLD, &sendReq); do { MPI_Test(&sendReq, &sendFlag, &sendStat); if (!doneWithRecvs) { tryRecv(recvReq, recvBuf); } } while (!sendFlag); }
int main(int argc, char* argv[]) { unsigned long long i, locN_U, locStart, locEnd; rndtype r, recvBuf[2]; MPI_Request recvReq; int base, iter, errorCnt = 0, totalErrors; double starttime, finishtime, exectime; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &nprocs); parseArgs(argc, argv); if (rank == 0) { printf("Number of nodes = %d\n", nprocs); fflush(stdout); printf("Problem size = %lld (2**%d)\n", m, n); fflush(stdout); printf("Number of updates = %lld (2**%d)\n", N_U, N_U_log2); fflush(stdout); } randInit(); A = malloc(sizeof(rndtype)*m/nprocs); if (A == NULL) { printf("malloc failed for main array.\n"); fflush(stdout); MPI_Abort(MPI_COMM_WORLD, 1); } base = rank * (m / nprocs); for (i=0; i < m / nprocs; i++) { A[i] = i + base; } MPI_Barrier(MPI_COMM_WORLD); // first iteration does the benchmark, second reverses it for validation. for (iter=0; iter < 2; iter++) { doneCount = 0; doneWithRecvs = 0; if (iter == 0) { starttime = now_time(); } MPI_Irecv(recvBuf, 2*sizeof(rndtype), MPI_BYTE, MPI_ANY_SOURCE, 1, MPI_COMM_WORLD, &recvReq); locN_U = N_U / nprocs; locStart = locN_U * rank; locEnd = locStart+locN_U; r = getNthRandom(locStart); for (i=locStart; i < locEnd; i++) { int loc = idxToLocale(r & idxMask); if (loc < 0 || loc > nprocs-1) { printf("error: r=%llu, r&mask=%llu, loc=%d\n", r, r&idxMask, loc); } if (loc == rank) { A[ind2localIdx(r & idxMask)] ^= r; } else { dosend(loc, r, 0, &recvReq, recvBuf); } getNextRandom(&r); } for (i = 0; i < nprocs; i++) { dosend(i, 0, 1, &recvReq, recvBuf); } while (!doneWithRecvs) { tryRecv(&recvReq, recvBuf); } MPI_Barrier(MPI_COMM_WORLD); if (iter == 0) { finishtime = now_time(); exectime = (finishtime - starttime) / 1.0e+6; } } for (i=0; i < m/nprocs; i++) { if (A[i] != i + base) { errorCnt++; } } MPI_Reduce(&errorCnt, &totalErrors, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD); if (rank == 0) { printf("Number of errors: %d\n", totalErrors); fflush(stdout); if (totalErrors <= errorTolerance * N_U) { printf("Validation: SUCCESS\n"); fflush(stdout); } else { printf("Validation: FAILURE\n"); fflush(stdout); } printf("Execution time = %lf\n", exectime); fflush(stdout); printf("Performance (GUPS) = %lf\n", (N_U / exectime) * 1.0e-9); fflush(stdout); } MPI_Finalize(); return 0; }
BOOL tryReadHeaders(Address *URL, Response *ret) { uint32_t recved; int rtl; static char buff[5] = {0}; if (!tryRecv(URL->Socket, buff, 5, &recved) || recved != 5) return FALSE; if (strncmp(buff, "HTTP/", 5) != 0) return ProtoError(); do { if (!tryRecv(URL->Socket, buff, 1, &recved) || recved != 1) return FALSE; GetNextDigit(&ret->HTTPMajorVer, buff); } while (IsDigit(buff[0]) == TRUE); if (buff[0] != '.') return ProtoError(); do { if (!tryRecv(URL->Socket, buff, 1, &recved) || recved != 1) return FALSE; GetNextDigit(&ret->HTTPMinorVer, buff); } while (IsDigit(buff[0]) == TRUE); if (buff[0] != ' ') return ProtoError(); if (!tryRecv(URL->Socket, buff, 3, &recved) || recved != 3) return FALSE; buff[3] = 0; ret->ResponceCode = atoi(buff); if (!tryRecv(URL->Socket, buff, 1, &recved) || recved != 1) return FALSE; ret->ResponceText = (char *)malloc(1); rtl = 0; do { if (!tryRecv(URL->Socket, buff, 1, &recved) || recved != 1) return FALSE; if (buff[0] != '\r' && buff[0] != '\n') { ret->ResponceText = (char *)realloc(ret->ResponceText, rtl + 2); ret->ResponceText[rtl] = buff[0]; rtl++; } } while (buff[0] != '\r' && buff[0] != '\n'); ret->ResponceText[rtl] = 0; if (!tryRecv(URL->Socket, buff + 1, 1, &recved) || recved != 1) return FALSE; if (!NewlineCheck(buff)) return FALSE; ret->Headers = malloc(0); while (1) { char *Header = malloc(1); int lenHeader = 0; Header[0] = 0; do { if (!tryRecv(URL->Socket, buff, 1, &recved) || recved != 1) return FALSE; if (buff[0] != '\r' && buff[0] != '\n') { Header = realloc(Header, lenHeader + 2); Header[lenHeader] = buff[0]; lenHeader++; } } while (buff[0] != ':' && buff[0] != '\r' && buff[0] != '\n'); Header[lenHeader] = 0; if (buff[0] == '\r' || buff[0] == '\n' && lenHeader == 0) { if (buff[0] == '\n') { free(Header); return HeaderProtoError(); } if (!tryRecv(URL->Socket, buff + 1, 1, &recved) || recved != 1) return FALSE; if (!NewlineCheck(buff)) return FALSE; break; } else { if (strcasecmp(Header, "Content-Length:") == 0) { if (!tryRecv(URL->Socket, buff, 1, &recved) || recved != 1) return FALSE; Header = realloc(Header, lenHeader + 2); Header[lenHeader] = buff[0]; lenHeader++; do { if (!tryRecv(URL->Socket, buff, 1, &recved) || recved != 1) return FALSE; if (IsDigit(buff[0]) == TRUE) { Header = (char *)realloc(Header, lenHeader + 2); Header[lenHeader] = buff[0]; lenHeader++; } GetNextDigit(&ret->ContentLen, buff); } while (IsDigit(buff[0]) == TRUE); Header[lenHeader] = 0; if (ret->Content != NULL) free(ret->Content); ret->Content = (char *)malloc(ret->ContentLen + 1); memset(ret->Content, 0x00, ret->ContentLen + 1); } else { if (strcasecmp(Header, "Transfer-Encoding:") == 0) ret->TransferEncodingFound = TRUE; do { if (!tryRecv(URL->Socket, buff, 1, &recved) || recved != 1) return FALSE; if (buff[0] != '\r' && buff[0] != '\n') { Header = (char *)realloc(Header, lenHeader + 2); Header[lenHeader] = buff[0]; lenHeader++; } } while (buff[0] != '\r' && buff[0] != '\n'); Header[lenHeader] = 0; } ret->Headers = (char **)realloc(ret->Headers, sizeof(char *) * (ret->nHeaders + 1)); ret->Headers[ret->nHeaders] = Header; ret->nHeaders++; if (!tryRecv(URL->Socket, buff + 1, 1, &recved) || recved != 1) return FALSE; if (!NewlineCheck(buff)) return FALSE; } } httpGetContentAsync(URL, ret); //recv(URL->Socket, ret->Content, ret->ContentLen, 0); return TRUE; }