changeset 1:3840d91821c4

VPThread version is working
author Merten Sach <msach@mailbox.tu-berlin.de>
date Tue, 16 Aug 2011 20:31:31 +0200
parents 4ae1d7ffb1ae
children 75c818c6cad1
files .hgignore Makefile c-ray-mt.c scene
diffstat 4 files changed, 91 insertions(+), 38 deletions(-) [+]
line diff
     1.1 --- a/.hgignore	Wed Aug 03 14:26:31 2011 +0200
     1.2 +++ b/.hgignore	Tue Aug 16 20:31:31 2011 +0200
     1.3 @@ -1,5 +1,6 @@
     1.4  syntax: glob
     1.5  
     1.6 +histograms
     1.7  nbproject
     1.8  c-ray-mt
     1.9  *.ppm
     2.1 --- a/Makefile	Wed Aug 03 14:26:31 2011 +0200
     2.2 +++ b/Makefile	Tue Aug 16 20:31:31 2011 +0200
     2.3 @@ -1,11 +1,37 @@
     2.4 -obj = c-ray-mt.o
     2.5 +obj = 	\
     2.6 +	VPThread_lib/VMS/Histogram/Histogram.o \
     2.7 +	VPThread_lib/VMS/Histogram/FloatHist.o \
     2.8 +	VPThread_lib/VMS/CoreLoop.o \
     2.9 +	VPThread_lib/VMS/VMS.o \
    2.10 +	VPThread_lib/VMS/MasterLoop.o \
    2.11 +	VPThread_lib/VMS/Queue_impl/PrivateQueue.o \
    2.12 +	VPThread_lib/VMS/Hash_impl/PrivateHash.o \
    2.13 +	VPThread_lib/VMS/DynArray/DynArray.o \
    2.14 +	VPThread_lib/VPThread_PluginFns.o \
    2.15 +	VPThread_lib/VPThread_lib.o \
    2.16 +	VPThread_lib/VMS/Histogram/DblHist.o \
    2.17 +	VPThread_lib/VPThread.o \
    2.18 +	VPThread_lib/VMS/probes.o \
    2.19 +	VPThread_lib/VMS/ProcrContext.o \
    2.20 +	VPThread_lib/VPThread_Request_Handlers.o \
    2.21 +	VPThread_lib/VPThread_helper.o \
    2.22 +	VPThread_lib/VMS/Hash_impl/MurmurHash2.o \
    2.23 +	VPThread_lib/VMS/vmalloc.o \
    2.24 +	VPThread_lib/VMS/contextSwitch.o \
    2.25 +	VPThread_lib/VMS/Queue_impl/BlockingQueue.o \
    2.26 +	VPThread_lib/VMS/vutilities.o \
    2.27 +	c-ray-mt.o
    2.28 +
    2.29  bin = c-ray-mt
    2.30  
    2.31  CC = gcc
    2.32 -CFLAGS = -O3 -ffast-math
    2.33 +CFLAGS = -m64 -ffast-math -fwrapv -fno-omit-frame-pointer -O3 -D VPTHREAD -D APPLICATION=C-RAY -g -Wall
    2.34  
    2.35  $(bin): $(obj)
    2.36  	$(CC) -o $@ $(obj) -lm -lpthread
    2.37 +	
    2.38 +%.o : %.c
    2.39 +	$(CC) -c $(CFLAGS) -o $@ $<
    2.40  
    2.41  .PHONY: clean
    2.42  clean:
    2.43 @@ -18,3 +44,6 @@
    2.44  .PHONY: uninstall
    2.45  uninstall:
    2.46  	rm -f /usr/local/bin/$(bin)
    2.47 +
    2.48 +
    2.49 +# $@ Name des Targets
     3.1 --- a/c-ray-mt.c	Wed Aug 03 14:26:31 2011 +0200
     3.2 +++ b/c-ray-mt.c	Tue Aug 16 20:31:31 2011 +0200
     3.3 @@ -85,11 +85,11 @@
     3.4  };
     3.5  
     3.6  struct thread_data {
     3.7 -	pthread_t tid;
     3.8 +	VirtProcr *VP;
     3.9  	int sl_start, sl_count;
    3.10 -
    3.11  	uint32_t *pixels;
    3.12  };
    3.13 +typedef struct thread_data thread_data;
    3.14  
    3.15  void render_scanline(int xsz, int ysz, int sl, uint32_t *fb, int samples);
    3.16  struct vec3 trace(struct ray ray, int depth);
    3.17 @@ -103,7 +103,7 @@
    3.18  void load_scene(FILE *fp);
    3.19  unsigned long get_msec(void);
    3.20  
    3.21 -void *thread_func(void *tdata);
    3.22 +void thread_func(void *tdata, VirtProcr *VProc);
    3.23  
    3.24  #define MAX_LIGHTS		16				/* maximum number of lights */
    3.25  #define RAY_MAG			1000.0			/* trace rays of this magnitude */
    3.26 @@ -145,9 +145,10 @@
    3.27  int thread_num = 1;
    3.28  struct thread_data *threads;
    3.29  
    3.30 -int start = 0;
    3.31 -pthread_mutex_t start_mutex = PTHREAD_MUTEX_INITIALIZER;
    3.32 -pthread_cond_t start_cond = PTHREAD_COND_INITIALIZER;
    3.33 +volatile int end = 0;
    3.34 +volatile int start = 0;
    3.35 +int32 end_mutex, end_cond;
    3.36 +int32 start_cond, start_mutex;
    3.37  
    3.38  #define NRAN	1024
    3.39  #define MASK	(NRAN - 1)
    3.40 @@ -168,12 +169,15 @@
    3.41  	"  -h         this help screen\n\n"
    3.42  };
    3.43  
    3.44 -void raytrace(uint32_t *pixels);
    3.45 +char __ProgrammName[] = "c-ray";
    3.46 +char __DataSet[255];
    3.47 +
    3.48 +
    3.49 +void raytrace(void *pixels, VirtProcr *Vprocr);
    3.50  
    3.51  int main(int argc, char **argv) {
    3.52  	int i;
    3.53  	uint32_t *pixels;
    3.54 -        double sl, sl_per_thread;
    3.55  	FILE *infile = stdin, *outfile = stdout;
    3.56  
    3.57  	for(i=1; i<argc; i++) {
    3.58 @@ -247,7 +251,8 @@
    3.59  	}
    3.60  	load_scene(infile);
    3.61          
    3.62 -        raytrace(pixels);
    3.63 +        //This is the transition to the VMS runtime
    3.64 +        VPThread__create_seed_procr_and_do_work(raytrace, (void*)pixels);
    3.65  	
    3.66  	/* output statistics to stderr */
    3.67  	fprintf(stderr, "Rendering took: %lu seconds (%lu milliseconds)\n", rend_time / 1000, rend_time);
    3.68 @@ -271,12 +276,11 @@
    3.69  		free(tmp);
    3.70  	}
    3.71  	free(pixels);
    3.72 -	free(threads);
    3.73  	return 0;
    3.74  }
    3.75  
    3.76  /* this is run after the VMS is set up*/
    3.77 -void raytrace(uint32_t *pixels)
    3.78 +void raytrace(void *pixels, VirtProcr *VProc)
    3.79  {
    3.80      int i;
    3.81      double sl, sl_per_thread;
    3.82 @@ -291,38 +295,53 @@
    3.83              thread_num = yres;
    3.84      }
    3.85  
    3.86 -    if(!(threads = malloc(thread_num * sizeof *threads))) {
    3.87 +    
    3.88 +    if(!(threads = VPThread__malloc(thread_num * sizeof(thread_data), VProc))) {
    3.89              perror("failed to allocate thread table");
    3.90              exit(EXIT_FAILURE);
    3.91      }
    3.92 -
    3.93 +    
    3.94 +    end_mutex = VPThread__make_mutex(VProc);
    3.95 +    end_cond  = VPThread__make_cond(end_mutex, VProc);
    3.96 +    start_mutex = VPThread__make_mutex(VProc);
    3.97 +    start_cond  = VPThread__make_cond(start_mutex, VProc);    
    3.98 +    
    3.99      sl = 0.0;
   3.100      sl_per_thread = (double)yres / (double)thread_num;
   3.101      for(i=0; i<thread_num; i++) {
   3.102              threads[i].sl_start = (int)sl;
   3.103              sl += sl_per_thread;
   3.104              threads[i].sl_count = (int)sl - threads[i].sl_start;
   3.105 -            threads[i].pixels = pixels;
   3.106 +            threads[i].pixels = (uint32_t*)pixels;
   3.107  
   3.108 -            if(pthread_create(&threads[i].tid, 0, thread_func, &threads[i]) != 0) {
   3.109 -                    perror("failed to spawn thread");
   3.110 -                    exit(EXIT_FAILURE);
   3.111 -            }
   3.112 +            threads[i].VP = 
   3.113 +                    VPThread__create_thread((VirtProcrFnPtr)thread_func,
   3.114 +                                   (void*)(&threads[i]), VProc);  
   3.115      }
   3.116 +    
   3.117      threads[thread_num - 1].sl_count = yres - threads[thread_num - 1].sl_start;
   3.118 -
   3.119 +    
   3.120      fprintf(stderr, VER_STR, VER_MAJOR, VER_MINOR);
   3.121 -
   3.122 -    pthread_mutex_lock(&start_mutex);
   3.123 +    
   3.124 +    // start worker threads
   3.125 +    //printf("start of worker thread (%d)\n", VProc->procrID);      
   3.126 +    VPThread__mutex_lock(start_mutex, VProc);
   3.127      start_time = get_msec();
   3.128      start = 1;
   3.129 -    pthread_cond_broadcast(&start_cond);
   3.130 -    pthread_mutex_unlock(&start_mutex);
   3.131 -
   3.132 -    for(i=0; i<thread_num; i++) {
   3.133 -            pthread_join(threads[i].tid, 0);
   3.134 -    }
   3.135 +    for(i=0; i<thread_num; i++)
   3.136 +        VPThread__cond_signal(start_cond, VProc);
   3.137 +    VPThread__mutex_unlock(start_mutex, VProc);
   3.138 +    
   3.139 +    //printf("wait for worker (%d)\n", VProc->procrID);      
   3.140 +    VPThread__mutex_lock(end_mutex, VProc);
   3.141 +    while(end < thread_num)
   3.142 +        VPThread__cond_wait(end_cond, VProc);
   3.143 +    VPThread__mutex_unlock(end_mutex, VProc);
   3.144 +    
   3.145      rend_time = get_msec() - start_time;
   3.146 +    
   3.147 +    VPThread__free(threads,VProc);
   3.148 +    VPThread__dissipate_thread(VProc);
   3.149  }
   3.150  
   3.151  /* render a frame of xsz/ysz dimensions into the provided framebuffer */
   3.152 @@ -674,19 +693,23 @@
   3.153  #error "I don't know how to measure time on your platform"
   3.154  #endif
   3.155  
   3.156 -void *thread_func(void *tdata) {
   3.157 +void thread_func(void *tdata, VirtProcr *VProc) {
   3.158  	int i;
   3.159  	struct thread_data *td = (struct thread_data*)tdata;
   3.160  
   3.161 -	pthread_mutex_lock(&start_mutex);
   3.162 -	while(!start) {
   3.163 -		pthread_cond_wait(&start_cond, &start_mutex);
   3.164 -	}
   3.165 -	pthread_mutex_unlock(&start_mutex);
   3.166 -
   3.167 +        VPThread__mutex_lock(start_mutex, VProc);
   3.168 +        while(!start)
   3.169 +            VPThread__cond_wait(start_cond, VProc);
   3.170 +        VPThread__mutex_unlock(start_mutex, VProc);        
   3.171 +        
   3.172  	for(i=0; i<td->sl_count; i++) {
   3.173  		render_scanline(xres, yres, i + td->sl_start, td->pixels, rays_per_pixel);
   3.174  	}
   3.175 +        
   3.176 +        VPThread__mutex_lock(end_mutex, VProc);
   3.177 +        end++;
   3.178 +        VPThread__cond_signal(end_cond, VProc);
   3.179 +        VPThread__mutex_unlock(end_mutex, VProc);   
   3.180  
   3.181 -	return 0;
   3.182 +	VPThread__dissipate_thread(VProc);
   3.183  }
     4.1 --- a/scene	Wed Aug 03 14:26:31 2011 +0200
     4.2 +++ b/scene	Tue Aug 16 20:31:31 2011 +0200
     4.3 @@ -7,7 +7,7 @@
     4.4  s	0 -1000 2		999		0.1 0.2 0.6			80.0	0.5
     4.5  
     4.6  # bouncing ball
     4.7 -s	0 0 2			1		1.0 0.5 0.1			60.0	0.7
     4.8 +s	0 0 2			1		0.0 0.0 0.0			60.0	0.7
     4.9  
    4.10  # lights...
    4.11  l	-50 100 -50