From 0652ee2715994b48a9bbfd1132bf0b65ad206289 Mon Sep 17 00:00:00 2001 From: Samo Penic <samo.penic@gmail.com> Date: Wed, 27 Nov 2013 18:46:13 +0000 Subject: [PATCH] commiting all changes. --- src/timestep.c | 77 ++++++++++++++++++++++++++------------ 1 files changed, 52 insertions(+), 25 deletions(-) diff --git a/src/timestep.c b/src/timestep.c index 30d6406..aad7817 100644 --- a/src/timestep.c +++ b/src/timestep.c @@ -2,7 +2,7 @@ #include<stdio.h> #include<math.h> #include<pthread.h> -#include<unistd.h> //usleep requires it +//#include<unistd.h> //usleep requires it //#include "io.h" #include "general.h" #include "timestep.h" @@ -22,16 +22,14 @@ pthread_t *tid=(pthread_t *)malloc(sizeof(pthread_t)*vesicle->threads); thdata *data=(thdata *)malloc(sizeof(thdata)*vesicle->threads); - pthread_mutex_t mutex_taint, mutex_untaint; - pthread_mutex_init(&mutex_taint,NULL); - pthread_mutex_init(&mutex_untaint,NULL); + pthread_mutex_init(&(vesicle->mutex->vtx_taint),NULL); + pthread_mutex_init(&(vesicle->mutex->vtx_untaint),NULL); + pthread_mutex_init(&(vesicle->mutex->cell_modify),NULL); for(i=0;i<vesicle->threads;i++){ data[i].thread_no=i; data[i].vesicle=vesicle; data[i].threads=vesicle->threads; - data[i].vtx_tainting_lock=&mutex_taint; - data[i].vtx_untainting_lock=&mutex_untaint; } for(i=0;i<inititer+iterations;i++){ @@ -51,8 +49,8 @@ } } - pthread_mutex_destroy(&mutex_taint); - pthread_mutex_destroy(&mutex_untaint); + pthread_mutex_destroy(&(vesicle->mutex->vtx_taint)); + pthread_mutex_destroy(&(vesicle->mutex->vtx_untaint)); pthread_exit(NULL); return TS_SUCCESS; } @@ -70,49 +68,78 @@ end=(thID+1)*partition_size; } ts_double rnvec[3]; - ts_uint i,j; //b; + ts_uint i,j,b; + ts_vertex *vtx; + //ts_bool retval; // ts_fprintf(stdout,"Thread thID=%d report for duty. My vtxes are in range from %d to %d.\n",thID,thID*partition_size, end-1); for(i=thID*partition_size;i<end;i++){ rnvec[0]=drand48(); rnvec[1]=drand48(); rnvec[2]=drand48(); +//TODO: you only need to taint (lock) vertices, that could be shared with another partition. You can leave out the rest. How can you test for that? Well, if any of vtx neighbours are in other partition, then you need to lock vertex and that neighbor. Otherwise not! /**** THREAD IS POTENTIALLY LOCKED ******/ - pthread_mutex_lock(data->vtx_tainting_lock); //taint if no other process is tainting or wait until you can taint + pthread_mutex_lock(&vesicle->mutex->vtx_taint); //taint if no other process is tainting or wait until you can taint // ts_fprintf(stdout, "thID=%d:: Tainting vertex %d, level=%d. Waiting....\n",thID, i, vesicle->vlist->vtx[i]->locked); while(vertex_tainted(vesicle->vlist->vtx[i],1,1)){ - ts_fprintf(stdout, "thID=%d:: Vertex %d is tainted, so I cannot try vertexmove. Level=%d. BU(H)TELJ!\n neigh. vtxs: ",thID, i, vesicle->vlist->vtx[i]->locked); + ts_fprintf(stdout, "thID=%d:: Vertex %d is tainted, so I cannot try vertexmove. Amount=%d. BU(H)TELJ!\n neigh. vtxs: ",thID, i, vesicle->vlist->vtx[i]->locked); for(j=0; j<vesicle->vlist->vtx[i]->neigh_no; j++){ - ts_fprintf(stdout, "%d ", vesicle->vlist->vtx[i]->neigh[j]->idx); + ts_fprintf(stdout, "%d(a=%d) ", vesicle->vlist->vtx[i]->neigh[j]->idx, vesicle->vlist->vtx[i]->neigh[j]->locked); } - ts_fprintf(stdout, ".\n"); + ts_fprintf(stdout, ".\n"); } - pthread_mutex_lock(data->vtx_untainting_lock); - vertex_taint(vesicle->vlist->vtx[i],2); - pthread_mutex_unlock(data->vtx_untainting_lock); + + pthread_mutex_lock(&vesicle->mutex->vtx_untaint); + vertex_taint(vesicle->vlist->vtx[i],1); + pthread_mutex_unlock(&vesicle->mutex->vtx_untaint); - pthread_mutex_unlock(data->vtx_tainting_lock); + pthread_mutex_unlock(&vesicle->mutex->vtx_taint); /**** THREAD IS RELEASING MUTEX RESOURCES ******/ single_verticle_timestep(vesicle,vesicle->vlist->vtx[i],rnvec); - pthread_mutex_lock(data->vtx_untainting_lock); - vertex_untaint(vesicle->vlist->vtx[i],2); - pthread_mutex_unlock(data->vtx_untainting_lock); -// ts_fprintf(stdout, "Vertex %d should be untainted, level=%d.\n", i, vesicle->vlist->vtx[i]->locked); + pthread_mutex_lock(&vesicle->mutex->vtx_untaint); + vertex_untaint(vesicle->vlist->vtx[i],1); + pthread_mutex_unlock(&vesicle->mutex->vtx_untaint); } // ts_int cnt=0; -/* - for(i=0;i<vesicle->vlist->n;i++){ + + for(i=0;i<(ts_uint)((ts_double)(vesicle->vlist->n)/(ts_double)vesicle->threads);i++){ b=rand() % vesicle->blist->n; //find a bond and return a pointer to a bond... //call single_bondflip_timestep... - retval=single_bondflip_timestep(vesicle,vesicle->blist->bond[b],rnvec); + vtx=vesicle->blist->bond[b]->vtx1; + + /**** THREAD IS POTENTIALLY LOCKED ******/ + pthread_mutex_lock(&vesicle->mutex->vtx_taint); + while(vertex_tainted(vtx,2,1)){ + ts_fprintf(stdout, "BF:: thID=%d Vertex %d is tainted. Amount=%d. BU(H)TELJ!\n neigh. vtxs: ",thID, i, *(vesicle->vlist->vtx[i]->locked)); + for(j=0; j<vesicle->vlist->vtx[i]->neigh_no; j++){ + ts_fprintf(stdout, "%d(a=%d) ", vesicle->vlist->vtx[i]->neigh[j]->idx, *(vesicle->vlist->vtx[i]->neigh[j]->locked)); + } + ts_fprintf(stdout, ".\n"); + } + + pthread_mutex_lock(&vesicle->mutex->vtx_untaint); + vertex_taint(vtx,2); + pthread_mutex_unlock(&vesicle->mutex->vtx_untaint); + + pthread_mutex_unlock(&vesicle->mutex->vtx_taint); + /**** THREAD IS RELEASING MUTEX RESOURCES ******/ + + //retval= + single_bondflip_timestep(vesicle,vesicle->blist->bond[b],rnvec); + + pthread_mutex_lock(&vesicle->mutex->vtx_untaint); + vertex_untaint(vtx,2); + pthread_mutex_unlock(&vesicle->mutex->vtx_untaint); + + // if(retval==TS_SUCCESS) cnt++; } // printf("Bondflip success rate in one sweep: %d/%d=%e\n", cnt,vesicle->blist->n,(double)cnt/(double)vesicle->blist->n); -*/ + /* if(retval); return TS_SUCCESS; */ -- Gitblit v1.9.3