X-Git-Url: https://hackdaworld.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=moldyn.c;h=13d7e5d60384039160b3be934952858c8859ade6;hb=4416acfc0eb87fa9d6a5e3671e22181591836fe9;hp=8d50a69c8a4f06b28d3a75ffcc57951c0281fbc5;hpb=452a348351ae8a2255809918a05683d2510655ce;p=physik%2Fposic.git diff --git a/moldyn.c b/moldyn.c index 8d50a69..13d7e5d 100644 --- a/moldyn.c +++ b/moldyn.c @@ -23,7 +23,7 @@ #include #endif -#ifdef PTHREADS +#if defined PTHREADS || defined VISUAL_THREAD #include #endif @@ -47,6 +47,12 @@ #undef PSE_NAME #undef PSE_COL +#ifdef PTHREADS +/* global mutexes */ +pthread_mutex_t *amutex; +pthread_mutex_t emutex; +#endif + /* * the moldyn functions */ @@ -66,13 +72,28 @@ int moldyn_init(t_moldyn *moldyn,int argc,char **argv) { rand_init(&(moldyn->random),NULL,1); moldyn->random.status|=RAND_STAT_VERBOSE; +#ifdef PTHREADS + pthread_mutex_init(&emutex,NULL); +#endif + return 0; } int moldyn_shutdown(t_moldyn *moldyn) { +#ifdef PTHREADS + int i; +#endif + printf("[moldyn] shutdown\n"); +#ifdef PTHREADS + for(i=0;icount;i++) + pthread_mutex_destroy(&(amutex[i])); + free(amutex); + pthread_mutex_destroy(&emutex); +#endif + moldyn_log_shutdown(moldyn); link_cell_shutdown(moldyn); rand_close(&(moldyn->random)); @@ -500,6 +521,9 @@ int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass, void *ptr; t_atom *atom; char name[16]; +#ifdef PTHREADS + pthread_mutex_t *mutex; +#endif new=a*b*c; count=moldyn->count; @@ -522,6 +546,16 @@ int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass, moldyn->atom=ptr; atom=&(moldyn->atom[count]); +#ifdef PTHREADS + ptr=realloc(amutex,(count+new)*sizeof(pthread_mutex_t)); + if(!ptr) { + perror("[moldyn] mutex realloc (add atom)"); + return -1; + } + amutex=ptr; + mutex=&(amutex[count]); +#endif + /* no atoms on the boundaries (only reason: it looks better!) */ if(!origin) { orig.x=0.5*lc; @@ -579,6 +613,9 @@ int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass, atom[ret].tag=count+ret; check_per_bound(moldyn,&(atom[ret].r)); atom[ret].r_0=atom[ret].r; +#ifdef PTHREADS + pthread_mutex_init(&(mutex[ret]),NULL); +#endif } /* update total system mass */ @@ -613,6 +650,16 @@ int add_atom(t_moldyn *moldyn,int element,double mass,u8 brand,u8 attr, moldyn->lc.subcell->list=ptr; #endif +#ifdef PTHREADS + ptr=realloc(amutex,(count+1)*sizeof(pthread_mutex_t)); + if(!ptr) { + perror("[moldyn] mutex realloc (add atom)"); + return -1; + } + amutex=ptr; + pthread_mutex_init(&(amutex[count]),NULL); +#endif + atom=moldyn->atom; /* initialize new atom */ @@ -1624,7 +1671,7 @@ int moldyn_integrate(t_moldyn *moldyn) { struct timeval t1,t2; //double tp; -#ifdef PTHREADS +#ifdef VISUAL_THREAD u8 first,change; pthread_t io_thread; int ret; @@ -1684,7 +1731,7 @@ int moldyn_integrate(t_moldyn *moldyn) { moldyn->debug=0; /* zero & init moldyn copy */ -#ifdef PTHREADS +#ifdef VISUAL_THREAD memset(&md_copy,0,sizeof(t_moldyn)); atom_copy=malloc(moldyn->count*sizeof(t_atom)); if(atom_copy==NULL) { @@ -1693,6 +1740,11 @@ int moldyn_integrate(t_moldyn *moldyn) { } #endif +#ifdef PTHREADS + printf("##################\n"); + printf("# USING PTHREADS #\n"); + printf("##################\n"); +#endif /* tell the world */ printf("[moldyn] integration start, go get a coffee ...\n"); @@ -1792,7 +1844,7 @@ int moldyn_integrate(t_moldyn *moldyn) { } if(a) { if(!(moldyn->total_steps%a)) { -#ifdef PTHREADS +#ifdef VISUAL_THREAD /* check whether thread has not terminated yet */ if(!first) { ret=pthread_join(io_thread,NULL); @@ -1830,7 +1882,7 @@ int moldyn_integrate(t_moldyn *moldyn) { /* get current time */ gettimeofday(&t2,NULL); -printf("\rsched:%d, steps:%d/%d, T:%4.1f/%4.1f P:%4.1f/%4.1f V:%6.1f (%d)", +printf("sched:%d, steps:%d/%d, T:%4.1f/%4.1f P:%4.1f/%4.1f V:%6.1f (%d)\n", sched->count,i,moldyn->total_steps, moldyn->t,moldyn->t_avg, moldyn->p/BAR,moldyn->p_avg/BAR, @@ -1951,11 +2003,6 @@ int potential_force_calc(t_moldyn *moldyn) { #endif u8 bc_ij,bc_ik; int dnlc; -#ifdef PTHREADS - int ret; - pthread_t kthread[27]; - t_kdata kdata[27]; -#endif count=moldyn->count; itom=moldyn->atom; @@ -1964,10 +2011,6 @@ int potential_force_calc(t_moldyn *moldyn) { atom=moldyn->atom; #endif -#ifdef PTHREADS - memset(kdata,0,27*sizeof(t_kdata)); -#endif - /* reset energy */ moldyn->energy=0.0; @@ -2136,9 +2179,7 @@ int potential_force_calc(t_moldyn *moldyn) { continue; /* first loop over atoms k */ -#ifndef PTHREADS if(moldyn->func3b_k1) { -#endif for(k=0;k<27;k++) { @@ -2177,24 +2218,11 @@ int potential_force_calc(t_moldyn *moldyn) { if(ktom==&(itom[i])) continue; -#ifdef PTHREADS - kdata[k].moldyn=moldyn; - kdata[k].ai=&(itom[i]); - kdata[k].aj=jtom; - kdata[k].ak=ktom; - kdata[k].bc=bc_ik; - ret=pthread_create(&(kthread[k]),NULL,moldyn->func3b_k1,&(kdata[k])); - if(ret) { - perror("[moldyn] create k1 thread"); - return ret; - } -#else moldyn->func3b_k1(moldyn, &(itom[i]), jtom, ktom, bc_ik|bc_ij); -#endif #ifdef STATIC_LISTS } @@ -2207,9 +2235,7 @@ int potential_force_calc(t_moldyn *moldyn) { } -#ifndef PTHREADS } -#endif if(moldyn->func3b_j2) moldyn->func3b_j2(moldyn, @@ -3000,7 +3026,7 @@ int visual_bonds_process(t_moldyn *moldyn,t_atom *itom,t_atom *jtom, return 0; } -#ifdef PTHREADS +#ifdef VISUAL_THREAD void *visual_atoms(void *ptr) { #else int visual_atoms(t_moldyn *moldyn) { @@ -3013,7 +3039,7 @@ int visual_atoms(t_moldyn *moldyn) { t_visual *v; t_atom *atom; t_vb vb; -#ifdef PTHREADS +#ifdef VISUAL_THREAD t_moldyn *moldyn; moldyn=ptr; @@ -3031,7 +3057,7 @@ int visual_atoms(t_moldyn *moldyn) { vb.fd=open(file,O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR); if(vb.fd<0) { perror("open visual save file fd"); -#ifndef PTHREADS +#ifndef VISUAL_THREAD return -1; #endif } @@ -3053,7 +3079,9 @@ int visual_atoms(t_moldyn *moldyn) { atom[i].ekin); // bonds between atoms +#ifndef VISUAL_THREAD process_2b_bonds(moldyn,&vb,visual_bonds_process); +#endif // boundaries if(dim.x) { @@ -3099,7 +3127,7 @@ int visual_atoms(t_moldyn *moldyn) { close(vb.fd); -#ifdef PTHREADS +#ifdef VISUAL_THREAD pthread_exit(NULL); }