void *work_thread (void *lp) { int task_id = *((int *) lp); int begin, end; struct timeval start, finish; int i; /*get the divided task*/ begin = (nsize * task_id) / task_num + 1; end = (nsize * (task_id + 1)) / task_num; if(task_id==0) gettimeofday (&start, NULL); fprintf (stderr, "thread %d: begin %d, end %d\n", task_id, begin, end); barrier (task_num); /* initialization */ if (task_id == 0) initRHS(nsize, begin, end); barrier (task_num); initResult(nsize, begin, end); barrier (task_num); /* Gauss Compute */ computeGauss(nsize, task_id); barrier (task_num); /* Gauss computation done */ if(task_id==0) { /* Since there are dependencies in computing equation stage the upper part need the results of upper part), it should be done by thread 0 solely. */ solveGauss(nsize); gettimeofday (&finish, NULL); printf ("Elapsed time: %.2f seconds\n", (((finish.tv_sec * 1000000.0) + finish.tv_usec) - ((start.tv_sec * 1000000.0) + start.tv_usec)) / 1000000.0); } }
void ABoxDlg::onStubCol( wxListEvent& event ) { if(m_SortCol == event.GetColumn()) ms_Sort = !ms_Sort; else ms_Sort = true; m_SortCol = event.GetColumn(); initResult(); }
int main(int argc, char *argv[]) { int i; struct timeval start, finish; double error; pthread_attr_t attr; pthread_t *tid; int *id; if (argc < 2) { fprintf(stderr, "usage: %s <matrixfile>\n", argv[0]); exit(-1); } // for getting the threads if(argc == 3) { task_num = strtol(argv[2], NULL, 10); } nsize = initMatrix(argv[1]); initRHS(nsize); initResult(nsize); // create threads id = (int *) malloc (sizeof (int) * task_num); tid = (pthread_t *) malloc (sizeof (pthread_t) * task_num); if (!id || !tid) errexit ("out of shared memory"); pthread_attr_init (&attr); pthread_attr_setscope (&attr, PTHREAD_SCOPE_SYSTEM); for (i = 1; i < task_num; i++) { id[i] = i; pthread_create (&tid[i], &attr, work_thread, &id[i]); } id[0]=0; work_thread(&id[0]); // wait for all threads to finish for (i = 1; i < task_num; i++) pthread_join (tid[i], NULL); solveGauss(nsize); error = 0.0; for (i = 0; i < nsize; i++) { double error__ = (X__[i]==0.0) ? 1.0 : fabs((X[i]-X__[i])/X__[i]); if (error < error__) { error = error__; } } fprintf(stdout, "Error: %e\n", error); }
int init() { int x1, x2; matrix1 = readMatrix("in1.txt", &m, &x1); if(matrix1 == NULL) return MATRIX1_ERROR; matrix2 = readMatrix("in2.txt", &x2, &n); if(matrix2 == NULL) return MATRIX2_ERROR; if(x1 != x2) return INCOMPATIBLE_ERROR; x = x1; resultMatrix = initResult(); return SUCCESS; }
void ABoxDlg::onShowPath( wxCommandEvent& event ) { wABox.setshowpath(m_Ini, m_ShowPath->IsChecked()?True:False); initResult(); }
void ABoxDlg::event(iONode node) { char* s = NodeOp.base.toString(node); TraceOp.trc( "aboxdlg", TRCLEVEL_INFO, __LINE__, 9999, "event: %.256s", s ); StrOp.free(s); if( wDataReq.getcmd(node) == wDataReq.abox_addlink ) { const char* uid = wDataReq.getid(node); iONode direntry = wDataReq.getdirentry(node); if( direntry != NULL && wDirEntry.getfileentry(direntry) != NULL ) { iONode fileentry = wDirEntry.getfileentry(direntry); TraceOp.trc( "aboxdlg", TRCLEVEL_INFO, __LINE__, 9999, "uid=%s for [%s] added=[%s]", uid, wFileEntry.getfname(fileentry), m_AddedFilename ); /* char* s = StrOp.fmt("%s:\nUID=%s\n%s=%s", wxGetApp().getCMsg("upload"), uid, wxGetApp().getCMsg("file"), wFileEntry.getfname(fileentry) ); int action = wxMessageDialog( this, wxString(s,wxConvUTF8), _T("Rocrail"), wxOK ).ShowModal(); StrOp.free(s); */ if( StrOp.equals( wFileEntry.getfname(fileentry), m_AddedFilename ) ) { TraceOp.trc( "aboxdlg", TRCLEVEL_INFO, __LINE__, 9999, "start upload of [%s]...", wFileEntry.getfname(fileentry) ); StrOp.copy( m_AddedUID, uid ); iONode cmd = NodeOp.inst( wDataReq.name(), NULL, ELEMENT_NODE ); wDataReq.setcmd( cmd, wDataReq.abox_filedata ); wDataReq.setid( cmd, uid ); wDataReq.setcategory(cmd, wFileEntry.getcategory(fileentry)); wDataReq.setfilename(cmd, FileOp.ripPath(m_AddedFilename)); wDataReq.setdatapart(cmd, 0); if( readDataBlock(wFileEntry.getfname(fileentry), cmd, 0) ) { m_AddedFilename[0] = '\0'; m_AddedUID[0] = '\0'; EnableDlg(true); } wxGetApp().sendToRocrail( cmd ); cmd->base.del(cmd); } } } else if( wDataReq.getcmd(node) == wDataReq.abox_getdata ) { TraceOp.trc( "aboxdlg", TRCLEVEL_INFO, __LINE__, 9999, "getdata..." ); /* * <datareq cmd="11" id="20150816093859345" category="Zomaar" filename="paspoort-2014.jpeg" datapart="0" controlcode="" server="infw5601F5A0" data="FFD8FFE0 */ if( StrOp.equals(m_DownloadUID, wDataReq.getid(node)) ) { if(wDataReq.getrc(node) != 0) { m_DownloadFilename[0] = '\0'; m_DownloadUID[0] = '\0'; m_DownloadPart = -1; m_labDownloadState->SetLabel(wxT("")); EnableDlg(true); int action = wxMessageDialog( this, wxT("Error getting file data!"), _T("Rocrail"), wxOK | wxICON_EXCLAMATION ).ShowModal(); return; } wxString tempdir = wxFileName::GetTempDir(); char* filepath = StrOp.fmt("%s%c%s", (const char*)tempdir.mb_str(wxConvUTF8), SystemOp.getFileSeparator(), wDataReq.getfilename(node) ); iOFile f = FileOp.inst(filepath, OPEN_APPEND); if( f != NULL ) { const char* byteStr = wDataReq.getdata(node); byte* filedata = StrOp.strToByte( byteStr ); int len = StrOp.len(byteStr)/2; FileOp.write(f, (char*)filedata, len); FileOp.base.del(f); freeMem(filedata); } if( !wDataReq.isack(node) ) { // get next part iONode cmd = NodeOp.inst( wDataReq.name(), NULL, ELEMENT_NODE ); wDataReq.setcmd( cmd, wDataReq.abox_getdata ); wDataReq.setid( cmd, wDataReq.getid(node) ); wDataReq.setcategory( cmd, wDataReq.getcategory(node) ); wDataReq.setfilename(cmd, wDataReq.getfilename(node) ); m_DownloadPart++; m_labDownloadState->SetLabel(wxString::Format(wxT("%d"), m_DownloadPart)); wDataReq.setdatapart(cmd, m_DownloadPart); wxGetApp().sendToRocrail( cmd ); cmd->base.del(cmd); } else { m_DownloadFilename[0] = '\0'; m_DownloadUID[0] = '\0'; m_DownloadPart = -1; m_labDownloadState->SetLabel(wxT("")); EnableDlg(true); executeStub(filepath); } StrOp.free(filepath); } } else if( wDataReq.getcmd(node) == wDataReq.abox_filedata ) { TraceOp.trc( "aboxdlg", TRCLEVEL_INFO, __LINE__, 9999, "filedata..." ); // <datareq cmd="10" id="20150816081059871" category="Zomaar" datapart="0" dataparts="33" totalsize="794973" data="" // controlcode="" server="infw2368A4D0" ack="true"/> if( wDataReq.isack(node) ) { if( StrOp.equals(m_AddedUID, wDataReq.getid(node) ) ) { iONode cmd = NodeOp.inst( wDataReq.name(), NULL, ELEMENT_NODE ); wDataReq.setcmd( cmd, wDataReq.abox_filedata ); wDataReq.setid( cmd, wDataReq.getid(node) ); wDataReq.setcategory(cmd, wDataReq.getcategory(node)); wDataReq.setfilename(cmd, wDataReq.getfilename(node)); wDataReq.setdatapart(cmd, wDataReq.getdatapart(node)+1); m_labUploadState->SetLabel(wxString::Format(wxT("%d"), wDataReq.getdatapart(cmd))); if( readDataBlock(m_AddedFilename, cmd, wDataReq.getdatapart(cmd)) ) { m_AddedFilename[0] = '\0'; m_AddedUID[0] = '\0'; m_labUploadState->SetLabel(wxT("")); EnableDlg(true); } wxGetApp().sendToRocrail( cmd ); cmd->base.del(cmd); } } } else if( wDataReq.getcmd(node) == wDataReq.abox_getcategories ) { m_ReadOnly = wDataReq.isreadonly(node)?true:false; if( m_Enable ) m_Add->Enable(!m_ReadOnly); m_Category->Clear(); wxString findtext = m_FindText->GetValue(); m_FindText->Clear(); m_FindText->SetValue(findtext); iOStrTok tok = StrTokOp.inst( wDataReq.getcategory( node ), ',' ); while( StrTokOp.hasMoreTokens(tok) ) { const char* category = StrTokOp.nextToken( tok ); m_Category->Append( wxString(category,wxConvUTF8) ); m_FindText->Append( wxString(category,wxConvUTF8) ); } } else if( wDataReq.getcmd(node) == wDataReq.abox_find ) { m_SelectedStub = wxNOT_FOUND; m_Open->Enable(false); m_Modify->Enable(false); m_Delete->Enable(false); m_Stubs->DeleteAllItems(); clearStubList(); char* s = NodeOp.base.toString(node); TraceOp.trc( "aboxdlg", TRCLEVEL_INFO, __LINE__, 9999, "found [%s]", s ); StrOp.free(s); iONode stub = NodeOp.findNode( node, "stub"); while( stub != NULL ) { iONode clone = (iONode)NodeOp.base.clone(stub); ListOp.add(m_StubList, (obj)clone); stub = NodeOp.findNextNode( node, stub); } initResult(); if(wDataReq.istoomanyhits(node)) { // Show warning. int action = wxMessageDialog( this, wxGetApp().getMsg("toomanyhits"), _T("Rocrail"), wxOK | wxICON_EXCLAMATION ).ShowModal(); } } }
int main(int argc, char **argv){ setbuf(stdout,0); int num = sysconf(_SC_NPROCESSORS_CONF); printf("system has %d processor(s)\n", num); cpu_set_t mask,get; CPU_ZERO(&mask); CPU_SET(0, &mask); int i,j ; int ite = 0; ite_converge = 0; hrtimer_converge = 0.0; int retcode,allone; void *retval; double hrtimer_tmp,hrtime_start,hrtime_end,l2normtotal; if ( argc != 4) {printf("[usage] SOR <matrix> <thread> <w>\n"); return 0;} if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0) { fprintf(stderr, "set thread affinity failed\n"); } thread_num = atoi(argv[2]); w = atof(argv[3]); nsize = initMatrix(argv[1]); initRHS(nsize); initResult(nsize); int workload = (int)ceil((double)nsize/(double)thread_num) ; pthread_t thread[thread_num]; int *seq = (int *)malloc(thread_num*sizeof(int)); for(i=0;i<thread_num;i++) seq[i] = i; shuffle(seq,thread_num); for(j=1;j<thread_num;j++) { retcode = pthread_create(&thread[j], NULL, task, new int(j)); if (retcode != 0) fprintf (stderr, "create thread failed %d\n", retcode); } sense_reverse_barrier(0); // start at the same time hrtime_start = gethrtime_x86(); printf("Finish reading file. Start computing...\n"); while(converge == 0) { ticket_acquire(&flags[0].tlock); if(flags[0].test == 1) { solve(0,workload-1,0,1); flags[0].flag = 0; flags[0].test = 0; sense_reverse_barrier(0); } else { hrtimer_tmp = gethrtime_x86(); ite ++; solve(0,workload-1,0,0); hrtimer_compute[0] += gethrtime_x86() - hrtimer_tmp; } if(token == 0 && flags[0].flag == 1&&converge==0) { //observe all flags allone = 1; for(j=0;j<thread_num;j++) { allone = allone & flags[j].flag; } if(allone == 1) { ite_converge ++; shuffle(seq,thread_num); for(i=0;i<thread_num;i++) if(seq[i] != 0) { ticket_acquire(&flags[seq[i]].tlock); flags[seq[i]].test = 1; ticket_release(&flags[seq[i]].tlock); } hrtimer_tmp = gethrtime_x86(); solve(0,workload-1,0,1); hrtimer_converge += gethrtime_x86() - hrtimer_tmp; sense_reverse_barrier(0); l2normtotal = 0.0; for(j=0;j<thread_num;j++) l2normtotal += flags[j].l2norm; if(l2normtotal < EPS) { converge = 1; } else { token = (token + 1) % thread_num; } } } ticket_release(&flags[0].tlock); } printf ("#%d finished\n",0); iteration[0] = ite; for(j=1;j<thread_num;j++) { retcode = pthread_join(thread[j], &retval); iteration[j] = *(int *)retval; if (retcode != 0) fprintf (stderr, "join failed %d\n", retcode); } hrtime_end = gethrtime_x86(); CPU_ZERO(&get); if (pthread_getaffinity_np(pthread_self(), sizeof(get), &get) < 0) fprintf(stderr, "get thread affinity failed\n"); for (j = 0; j < 24; j++) if (CPU_ISSET(j, &get)) printf("thread %d is running in processor %d sched_getcpu %d\n", -1, j,sched_getcpu()); #ifdef VERIFY FILE *res; res = fopen ("ASOR result", "w"); for (i = 0; i < nsize; i++) { fprintf (res, "[%d] = %lf\n", i+1, X[i]); } #endif double total = hrtime_end-hrtime_start; printf("Total time:%.16lfs\n",total); printf("Computation time:%.16lfs\n",max(thread_num,hrtimer_compute)); printf("Convergence time:%.16lfs\n",hrtimer_converge); printf("Syn time:%.16lfs\n",total-max(thread_num,hrtimer_compute)-hrtimer_converge); for(i=0;i<thread_num;i++) printf("Iteration[%d]: %d\t\tComputation time:%.16lfs\n",i,iteration[i],hrtimer_compute[i]); printf("#Converge iteration:%d\n",ite_converge); printf("sizeof(cflag) = %lu\n",sizeof(cflag)); return 0; }
int main(int argc, char *argv[]) { int i; struct timeval start, finish; double error; pthread_attr_t attr; pthread_t *tid; int *id; //Ensure that the program takes two parameters // first param is the input matrix and the second is the number of processors for the parallel run if (argc != 3) { fprintf(stderr, "usage: %s <matrixfile> <#ofProcesses>\n", argv[0]); exit(-1); } task_num = atoi(argv[2]); nsize = initMatrix(argv[1]); initRHS(nsize); initResult(nsize); gettimeofday(&start, 0); // create threads id = (int *) malloc (sizeof (int) * task_num); tid = (pthread_t *) malloc (sizeof (pthread_t) * task_num); if (!id || !tid) errexit ("out of shared memory"); pthread_attr_init (&attr); pthread_attr_setscope (&attr, PTHREAD_SCOPE_SYSTEM); for (i = 1; i < task_num; i++) { id[i] = i; pthread_create (&tid[i], &attr, work_thread, &id[i]); } id[0]=0; work_thread(&id[0]); // wait for all threads to finish for (i = 1; i < task_num; i++) pthread_join (tid[i], NULL); gettimeofday(&finish, 0); solveGauss(nsize); fprintf(stdout, "Time: %f seconds\n", (finish.tv_sec - start.tv_sec) + (finish.tv_usec - start.tv_usec)*0.000001); error = 0.0; for (i = 0; i < nsize; i++) { double error__ = (X__[i]==0.0) ? 1.0 : fabs((X[i]-X__[i])/X__[i]); if (error < error__) { error = error__; } } fprintf(stdout, "Error: %e\n", error); //File output // float timeOutput = ((finish.tv_sec - start.tv_sec) + (finish.tv_usec - start.tv_usec)*0.000001); // FILE *pthreadTimingFile = fopen("pthread1Timing.txt", "a"); // fprintf(pthreadTimingFile, "%s %d %f %G\n", argv[1], task_num, timeOutput, error); // fclose(pthreadTimingFile); return 0; }