From 4e56fe682e6ad34c161aa40c06af4e3e0088962e Mon Sep 17 00:00:00 2001
From: Samo Penic <samo.penic@gmail.com>
Date: Fri, 22 Nov 2013 19:29:14 +0000
Subject: [PATCH] Testing of paralel algorithm with mutexes and tainting. Still no success. It seems tainting and untainting does not works as suspected

---
 src/timestep.c |   47 +++++++++++++++++++++++++++++++++++------------
 1 files changed, 35 insertions(+), 12 deletions(-)

diff --git a/src/timestep.c b/src/timestep.c
index c38b305..3290184 100644
--- a/src/timestep.c
+++ b/src/timestep.c
@@ -2,6 +2,7 @@
 #include<stdio.h>
 #include<math.h>
 #include<pthread.h>
+#include<unistd.h> //usleep requires it
 //#include "io.h"
 #include "general.h"
 #include "timestep.h"
@@ -17,18 +18,35 @@
 	centermass(vesicle);
 	cell_occupation(vesicle);
 	ts_fprintf(stdout, "Starting simulation (first %d x %d MC sweeps will not be recorded on disk)\n", inititer, mcsweeps);
-	pthread_t tid1,tid2;
-	thdata data1, data2; 
+	pthread_t tid1,tid2, tid3, tid4;
+	thdata data1, data2, data3, data4;
+	pthread_mutex_t mutex;
 	data1.thread_no=0;
 	data2.thread_no=1;
 	data1.vesicle=vesicle;
 	data2.vesicle=vesicle;
-        	for(i=0;i<inititer+iterations;i++){
+       	data3.thread_no=2;
+	data4.thread_no=3;
+	data3.vesicle=vesicle;
+	data4.vesicle=vesicle;
+
+	data1.vtx_tainting_lock=&mutex;
+	data2.vtx_tainting_lock=&mutex;
+	data3.vtx_tainting_lock=&mutex;
+	data4.vtx_tainting_lock=&mutex;
+
+	pthread_mutex_init(&mutex,NULL);
+
+ 	for(i=0;i<inititer+iterations;i++){
 		for(j=0;j<mcsweeps;j++){
 			pthread_create(&tid1,NULL,single_timestep,(void *)&data1);
         		pthread_create(&tid2,NULL,single_timestep,(void *)&data2);
+        		pthread_create(&tid3,NULL,single_timestep,(void *)&data3);
+        		pthread_create(&tid4,NULL,single_timestep,(void *)&data4);
 		        pthread_join(tid1,NULL);
 		        pthread_join(tid2,NULL);
+		        pthread_join(tid3,NULL);
+		        pthread_join(tid4,NULL);
 
 //	single_timestep(vesicle);
 		}
@@ -38,6 +56,9 @@
 			write_vertex_xml_file(vesicle,i-inititer);
 		}
 	}
+
+	pthread_mutex_destroy(&mutex);
+	pthread_exit(NULL);
 	return TS_SUCCESS;
 }
 
@@ -46,9 +67,9 @@
 	data=(thdata *)thread_data;
 	ts_vesicle *vesicle=data->vesicle;
 	ts_uint thID=data->thread_no;
-	ts_uint partition_size=(ts_uint)(vesicle->vlist->n/2);
+	ts_uint partition_size=(ts_uint)(vesicle->vlist->n/4);
 	ts_uint end;
-	if(thID==2){
+	if(thID==3){
 		end=vesicle->vlist->n;
 	} else {
 		end=(thID+1)*partition_size;
@@ -59,14 +80,17 @@
         rnvec[0]=drand48();
         rnvec[1]=drand48();
         rnvec[2]=drand48();
-	vertex_taint(vesicle->vlist->vtx[i],1);
-//		ts_fprintf(stdout, "Vertex %d should be tainted, level=%d.\n", i, vesicle->vlist->vtx[i]->locked);
+
+	/**** THREAD IS POTENTIALLY LOCKED ******/
+	pthread_mutex_lock(data->vtx_tainting_lock); //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, "Vertex %d tainted, level=%d. Waiting....\n", i, vesicle->vlist->vtx[i]->locked);
-		vertex_untaint(vesicle->vlist->vtx[i],1);
-		//TODO: wait a random amount of time//
-		vertex_taint(vesicle->vlist->vtx[i],1);
+		ts_fprintf(stdout, "thID=%d:: Vertex %d is tainted, so I cannot try vertexmove. Level=%d. WAITING!\n",thID, i, vesicle->vlist->vtx[i]->locked);
 	}
+	vertex_taint(vesicle->vlist->vtx[i],1);
+	pthread_mutex_unlock(data->vtx_tainting_lock);
+	/**** THREAD IS RELEASING MUTEX RESOURCES ******/
+
         single_verticle_timestep(vesicle,vesicle->vlist->vtx[i],rnvec);
 	vertex_untaint(vesicle->vlist->vtx[i],1);
 //		ts_fprintf(stdout, "Vertex %d should be untainted, level=%d.\n", i, vesicle->vlist->vtx[i]->locked);
@@ -86,7 +110,6 @@
 /*	if(retval);
     return TS_SUCCESS;
 */
-
 	 pthread_exit(0); /* exit */
 }
 

--
Gitblit v1.9.3