From 99a9305339d568641a7ec55783faa161b3c33311 Mon Sep 17 00:00:00 2001 From: hackbard Date: Tue, 2 Sep 2008 01:17:00 +0200 Subject: [PATCH] introduced lowmem lists --- Makefile | 3 +- moldyn.c | 117 ++++++++++++++++++++++++++++++++++++----- moldyn.h | 10 ++++ potentials/albe_fast.c | 44 +++++++++++++--- 4 files changed, 152 insertions(+), 22 deletions(-) diff --git a/Makefile b/Makefile index d1b78ec..4de3dcc 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -CC = gcc +CC = gcc-4.3 CFLAGS = -Wall -Winline #CFLAGS += -Wextra -pedantic @@ -13,6 +13,7 @@ CFLAGS += -DALBE_FAST #CFLAGS += -DTERSOFF_ORIG #CFLAGS += -DSTATIC_LISTS +CFLAGS += -DLOWMEM_LISTS #CFLAGS += -DDEBUG #CFLAGS += -DDSTART=-1 -DDEND=40 -DDATOM=0 diff --git a/moldyn.c b/moldyn.c index 7a9709e..6da2b95 100644 --- a/moldyn.c +++ b/moldyn.c @@ -600,6 +600,15 @@ int add_atom(t_moldyn *moldyn,int element,double mass,u8 brand,u8 attr, } moldyn->atom=ptr; +#ifdef LOWMEM_LISTS + ptr=realloc(moldyn->lc.subcell->list,(count+1)*sizeof(int)); + if(!ptr) { + perror("[moldyn] list realloc (add atom)"); + return -1; + } + moldyn->lc.subcell->list=ptr; +#endif + atom=moldyn->atom; /* initialize new atom */ @@ -1293,7 +1302,9 @@ double estimate_time_step(t_moldyn *moldyn,double nn_dist) { int link_cell_init(t_moldyn *moldyn,u8 vol) { t_linkcell *lc; +#ifndef LOWMEM_LISTS int i; +#endif lc=&(moldyn->lc); @@ -1308,8 +1319,8 @@ int link_cell_init(t_moldyn *moldyn,u8 vol) { #ifdef STATIC_LISTS lc->subcell=malloc(lc->cells*sizeof(int*)); -#elif LOWMEM_LIST - lc->subcell=malloc(t_lowmem_list); +#elif LOWMEM_LISTS + lc->subcell=malloc(sizeof(t_lowmem_list)); #else lc->subcell=malloc(lc->cells*sizeof(t_list)); #endif @@ -1327,7 +1338,7 @@ int link_cell_init(t_moldyn *moldyn,u8 vol) { #ifdef STATIC_LISTS printf("[moldyn] initializing 'static' linked cells (%d)\n", lc->cells); -#elif LOWMEM_LIST +#elif LOWMEM_LISTS printf("[moldyn] initializing 'lowmem' linked cells (%d)\n", lc->cells); #else @@ -1353,7 +1364,7 @@ int link_cell_init(t_moldyn *moldyn,u8 vol) { i,lc->subcell[0],lc->subcell); */ } -#elif LOWMEM_LIST +#elif LOWMEM_LISTS lc->subcell->head=malloc(lc->cells*sizeof(int)); if(lc->subcell->head==NULL) { perror("[moldyn] head init (malloc)"); @@ -1378,23 +1389,25 @@ int link_cell_init(t_moldyn *moldyn,u8 vol) { int link_cell_update(t_moldyn *moldyn) { int count,i,j,k; - int nx,ny; + int nx,nxy; t_atom *atom; t_linkcell *lc; #ifdef STATIC_LISTS int p; +#elif LOWMEM_LISTS + int p; #endif atom=moldyn->atom; lc=&(moldyn->lc); nx=lc->nx; - ny=lc->ny; + nxy=nx*lc->ny; for(i=0;icells;i++) #ifdef STATIC_LISTS memset(lc->subcell[i],-1,(MAX_ATOMS_PER_LIST+1)*sizeof(int)); -#elif LOWMEM_LIST +#elif LOWMEM_LISTS lc->subcell->head[i]=-1; #else list_destroy_f(&(lc->subcell[i])); @@ -1407,7 +1420,7 @@ int link_cell_update(t_moldyn *moldyn) { #ifdef STATIC_LISTS p=0; - while(lc->subcell[i+j*nx+k*nx*ny][p]!=-1) + while(lc->subcell[i+j*nx+k*nxy][p]!=-1) p++; if(p>=MAX_ATOMS_PER_LIST) { @@ -1415,12 +1428,13 @@ int link_cell_update(t_moldyn *moldyn) { return -1; } - lc->subcell[i+j*nx+k*nx*ny][p]=count; -#elif LOWMEM_LIST - lc->subcell->list[count]=list->subcell->head[i+j*nx+k*nx*ny]; - list->subcell->head=count; + lc->subcell[i+j*nx+k*nxy][p]=count; +#elif LOWMEM_LISTS + p=i+j*nx+k*nxy; + lc->subcell->list[count]=lc->subcell->head[p]; + lc->subcell->head[p]=count; #else - list_add_immediate_f(&(lc->subcell[i+j*nx+k*nx*ny]), + list_add_immediate_f(&(lc->subcell[i+j*nx+k*nxy]), &(atom[count])); /* if(j==0&&k==0) @@ -1436,6 +1450,8 @@ int link_cell_update(t_moldyn *moldyn) { int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k, #ifdef STATIC_LISTS int **cell +#elif LOWMEM_LISTS + int *cell #else t_list *cell #endif @@ -1461,7 +1477,11 @@ int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k, printf("[moldyn] WARNING: lcni %d/%d %d/%d %d/%d\n", i,nx,j,ny,k,nz); +#ifndef LOWMEM_LISTS cell[0]=lc->subcell[i+j*nx+k*a]; +#else + cell[0]=lc->subcell->head[i+j*nx+k*a]; +#endif for(ci=-1;ci<=1;ci++) { bx=0; x=i+ci; @@ -1485,10 +1505,19 @@ int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k, } if(!(ci|cj|ck)) continue; if(bx|by|bz) { +#ifndef LOWMEM_LISTS cell[--count2]=lc->subcell[x+y*nx+z*a]; +#else + cell[--count2]=lc->subcell->head[x+y*nx+z*a]; +#endif + } else { +#ifndef LOWMEM_LISTS cell[count1++]=lc->subcell[x+y*nx+z*a]; +#else + cell[count1++]=lc->subcell->head[x+y*nx+z*a]; +#endif } } } @@ -1501,11 +1530,19 @@ int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k, int link_cell_shutdown(t_moldyn *moldyn) { +#ifndef LOWMEM_LISTS int i; +#endif t_linkcell *lc; lc=&(moldyn->lc); +#if LOWMEM_LISTS + free(lc->subcell->head); + free(lc->subcell->list); + +#else + for(i=0;icells;i++) { #ifdef STATIC_LISTS free(lc->subcell[i]); @@ -1514,6 +1551,7 @@ int link_cell_shutdown(t_moldyn *moldyn) { list_destroy_f(&(lc->subcell[i])); #endif } +#endif free(lc->subcell); @@ -1849,6 +1887,9 @@ int potential_force_calc(t_moldyn *moldyn) { int *neighbour_i[27]; int p,q; t_atom *atom; +#elif LOWMEM_LISTS + int neighbour_i[27]; + int p,q; #else t_list neighbour_i[27]; t_list neighbour_i2[27]; @@ -1929,6 +1970,13 @@ int potential_force_calc(t_moldyn *moldyn) { jtom=&(atom[neighbour_i[j][p]]); p++; +#elif LOWMEM_LISTS + p=neighbour_i[j]; + + while(p!=-1) { + + jtom=&(itom[p]); + p=lc->subcell->list[p]; #else this=&(neighbour_i[j]); list_reset_f(this); @@ -1952,6 +2000,8 @@ int potential_force_calc(t_moldyn *moldyn) { } #ifdef STATIC_LISTS } +#elif LOWMEM_LISTS + } #else } while(list_next_f(this)!=L_NO_NEXT_ELEMENT); #endif @@ -1967,6 +2017,8 @@ int potential_force_calc(t_moldyn *moldyn) { /* copy the neighbour lists */ #ifdef STATIC_LISTS /* no copy needed for static lists */ +#elif LOWMEM_LISTS + /* no copy needed for lowmem lists */ #else memcpy(neighbour_i2,neighbour_i,27*sizeof(t_list)); #endif @@ -1982,6 +2034,13 @@ int potential_force_calc(t_moldyn *moldyn) { jtom=&(atom[neighbour_i[j][p]]); p++; +#elif LOWMEM_LISTS + p=neighbour_i[j]; + + while(p!=-1) { + + jtom=&(itom[p]); + p=lc->subcell->list[p]; #else this=&(neighbour_i[j]); list_reset_f(this); @@ -2026,6 +2085,13 @@ int potential_force_calc(t_moldyn *moldyn) { ktom=&(atom[neighbour_i[k][q]]); q++; +#elif LOWMEM_LISTS + q=neighbour_i[k]; + + while(q!=-1) { + + ktom=&(itom[q]); + q=lc->subcell->list[q]; #else that=&(neighbour_i2[k]); list_reset_f(that); @@ -2053,6 +2119,8 @@ int potential_force_calc(t_moldyn *moldyn) { bc_ik|bc_ij); #ifdef STATIC_LISTS } +#elif LOWMEM_LISTS + } #else } while(list_next_f(that)!=\ L_NO_NEXT_ELEMENT); @@ -2081,6 +2149,13 @@ int potential_force_calc(t_moldyn *moldyn) { ktom=&(atom[neighbour_i[k][q]]); q++; +#elif LOWMEM_LISTS + q=neighbour_i[k]; + + while(q!=-1) { + + ktom=&(itom[q]); + q=lc->subcell->list[q]; #else that=&(neighbour_i2[k]); list_reset_f(that); @@ -2109,6 +2184,8 @@ int potential_force_calc(t_moldyn *moldyn) { #ifdef STATIC_LISTS } +#elif LOWMEM_LISTS + } #else } while(list_next_f(that)!=\ L_NO_NEXT_ELEMENT); @@ -2126,6 +2203,8 @@ int potential_force_calc(t_moldyn *moldyn) { } #ifdef STATIC_LISTS } +#elif LOWMEM_LISTS + } #else } while(list_next_f(this)!=L_NO_NEXT_ELEMENT); #endif @@ -2367,6 +2446,9 @@ int process_2b_bonds(t_moldyn *moldyn,void *data, #ifdef STATIC_LISTS int *neighbour[27]; int p; +#elif LOWMEM_LISTS + int neighbour[27]; + int p; #else t_list neighbour[27]; t_list *this; @@ -2397,6 +2479,13 @@ int process_2b_bonds(t_moldyn *moldyn,void *data, jtom=&(moldyn->atom[neighbour[j][p]]); p++; +#elif LOWMEM_LISTS + p=neighbour[j]; + + while(p!=-1) { + + jtom=&(itom[p]); + p=lc->subcell->list[p]; #else this=&(neighbour[j]); list_reset_f(this); @@ -2414,6 +2503,8 @@ int process_2b_bonds(t_moldyn *moldyn,void *data, #ifdef STATIC_LISTS } +#elif LOWMEM_LISTS + } #else } while(list_next_f(this)!=L_NO_NEXT_ELEMENT); #endif diff --git a/moldyn.h b/moldyn.h index c3b8c6b..da951b5 100644 --- a/moldyn.h +++ b/moldyn.h @@ -58,6 +58,12 @@ typedef struct s_atom { #define DEFAULT_ATOM_ATTR 0x74 // 1,2,3 body interaction + visualize +/* special list structure for low mem approach */ +typedef struct s_lowmem_list { + int *head; + int *list; +} t_lowmem_list; + /* cell lists */ typedef struct s_linkcell { int nx,ny,nz; /* amount of cells in x, y and z direction */ @@ -66,6 +72,8 @@ typedef struct s_linkcell { double x,y,z; /* the actual cell lengthes */ #ifdef STATIC_LISTS int **subcell; /* pointer to the cell lists */ +#elif LOWMEM_LISTS + t_lowmem_list *subcell; /* low mem approach list */ #else t_list *subcell; /* pointer to the cell lists */ #endif @@ -424,6 +432,8 @@ int link_cell_init(t_moldyn *moldyn,u8 vol); int link_cell_update(t_moldyn *moldyn); #ifdef STATIC_LISTS int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,int **cell); +#elif LOWMEM_LISTS +int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,int *cell); #else int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,t_list *cell); #endif diff --git a/potentials/albe_fast.c b/potentials/albe_fast.c index 4523938..45ebff9 100644 --- a/potentials/albe_fast.c +++ b/potentials/albe_fast.c @@ -45,7 +45,9 @@ int albe_potential_force_calc(t_moldyn *moldyn) { #ifdef STATIC_LISTS int *neighbour_i[27]; int p,q; - t_atom *atom; +#elif LOWMEM_LISTS + int neighbour_i[27]; + int p,q; #else t_list neighbour_i[27]; t_list neighbour_i2[27]; @@ -102,9 +104,6 @@ int albe_potential_force_calc(t_moldyn *moldyn) { count=moldyn->count; itom=moldyn->atom; lc=&(moldyn->lc); -#ifdef STATIC_LISTS - atom=moldyn->atom; -#endif // optimized params=moldyn->pot_params; @@ -157,7 +156,9 @@ int albe_potential_force_calc(t_moldyn *moldyn) { dnlc=lc->dnlc; /* copy the neighbour lists */ -#ifndef STATIC_LISTS +#ifdef STATIC_LISTS +#elif LOWMEM_LISTS +#else memcpy(neighbour_i2,neighbour_i,27*sizeof(t_list)); #endif @@ -173,8 +174,15 @@ int albe_potential_force_calc(t_moldyn *moldyn) { while(neighbour_i[j][p]!=-1) { - jtom=&(atom[neighbour_i[j][p]]); + jtom=&(itom[neighbour_i[j][p]]); p++; +#elif LOWMEM_LISTS + p=neighbour_i[j]; + + while(p!=-1) { + + jtom=&(itom[p]); + p=lc->subcell->list[p]; #else this=&(neighbour_i[j]); list_reset_f(this); @@ -238,8 +246,15 @@ int albe_potential_force_calc(t_moldyn *moldyn) { while(neighbour_i[k][q]!=-1) { - ktom=&(atom[neighbour_i[k][q]]); + ktom=&(itom[neighbour_i[k][q]]); q++; +#elif LOWMEM_LISTS + q=neighbour_i[k]; + + while(q!=-1) { + + ktom=&(itom[q]); + q=lc->subcell->list[q]; #else that=&(neighbour_i2[k]); list_reset_f(that); @@ -359,6 +374,8 @@ int albe_potential_force_calc(t_moldyn *moldyn) { #ifdef STATIC_LISTS } +#elif LOWMEM_LISTS + } #else } while(list_next_f(that)!=\ L_NO_NEXT_ELEMENT); @@ -468,8 +485,15 @@ if(moldyn->time>DSTART&&moldyn->timesubcell->list[q]; #else that=&(neighbour_i2[k]); list_reset_f(that); @@ -597,6 +621,8 @@ if(moldyn->time>DSTART&&moldyn->timetime>DSTART&&moldyn->time