From e6996e2b342bcffaa3dbe23f14ea1c509a6dc371 Mon Sep 17 00:00:00 2001 From: yurkin Date: Wed, 8 Feb 2012 06:52:44 +0000 Subject: [PATCH] Further work to improve handling of string literals throughout the code (continuation of r 1105). A few string functions in param.c were changed accordingly. Filenames, etc. now do not require additional storage but just point to respective parts of 'argv'. Automatic directory name was slightly changed, e.g., from '...g16m1_5[id...]' to '...g16_m1.5[_id...]'. Line in the beginning of stdout with a lot of "meaningless" numbers was removed (it was especially annoying in MPI mode). Now it is only produced in FULLDEBUG. Instead, box dimensions were added to stdout. Fixed minor bug in saving DDSCAT 7 format, introduced by original implementation of r1100. Minor changes in Stop() (comm.c) in parallel mode. --- src/GenerateB.c | 4 +- src/calculator.c | 4 +- src/comm.c | 11 +++-- src/cpp/fft_setup.cpp | 2 +- src/iterative.c | 3 ++ src/make_particle.c | 35 ++++++++------ src/param.c | 103 ++++++++++++++++++++++-------------------- src/vars.c | 10 ++-- src/vars.h | 4 +- 9 files changed, 95 insertions(+), 81 deletions(-) diff --git a/src/GenerateB.c b/src/GenerateB.c index 2529be00..6ab60e53 100644 --- a/src/GenerateB.c +++ b/src/GenerateB.c @@ -43,8 +43,8 @@ // defined and initialized in param.c extern const int beam_Npars; extern const double beam_pars[]; -extern const char beam_fnameY[]; -extern const char beam_fnameX[]; +extern const char *beam_fnameY; +extern const char *beam_fnameX; extern opt_index opt_beam; diff --git a/src/calculator.c b/src/calculator.c index 54b39812..24b0a98b 100644 --- a/src/calculator.c +++ b/src/calculator.c @@ -3,7 +3,7 @@ * Descr: all the initialization is done here before actually calculating internal fields; includes * calculation of couple constants * - * Copyright (C) 2006-2010 ADDA contributors + * Copyright (C) 2006-2010,2012 ADDA contributors * This file is part of ADDA. * * ADDA is free software: you can redistribute it and/or modify it under the terms of the GNU @@ -39,7 +39,7 @@ extern const Parms_1D parms[2],parms_alpha; extern const angle_set beta_int,gamma_int,theta_int,phi_int; // defined and initialized in param.c extern const int avg_inc_pol; -extern const char alldir_parms[],scat_grid_parms[]; +extern const char *alldir_parms,*scat_grid_parms; // defined and initialized in timing.c extern TIME_TYPE Timing_Init; extern size_t TotalEval; diff --git a/src/comm.c b/src/comm.c index f08371be..7686519f 100644 --- a/src/comm.c +++ b/src/comm.c @@ -3,7 +3,7 @@ * Descr: incorporates all parallelization related code, so most of it is directly involved in or * closely related to interprocess communication * - * Copyright (C) 2006-2011 ADDA contributors + * Copyright (C) 2006-2012 ADDA contributors * This file is part of ADDA. * * ADDA is free software: you can redistribute it and/or modify it under the terms of the GNU @@ -30,6 +30,7 @@ #include "function.h" #include "parbas.h" #include "cmplx.h" +#include "debug.h" #ifdef ADDA_MPI MPI_Datatype mpi_dcomplex,mpi_double3,mpi_dcomplex3; // combined datatypes @@ -294,9 +295,6 @@ void Stop(const int code) MPI_Abort(MPI_COMM_WORLD,code); } else { // regular termination - // wait for all processors - fflush(stdout); - Synchronize(); // clean MPI constructs and some memory MPI_Type_free(&mpi_dcomplex); MPI_Type_free(&mpi_double3); @@ -305,6 +303,9 @@ void Stop(const int code) Free_general(recvcounts); Free_general(displs); } + // wait for all processors + fflush(stdout); + Synchronize(); // finalize MPI communications MPI_Finalize(); } @@ -581,7 +582,7 @@ void ParSetup(void) local_Nx=local_x1-local_x0; boxXY=boxX*(size_t)boxY; // overflow check is covered by gridYZ above local_Ndip=MultOverflow(boxXY,local_z1_coer-local_z0,ALL_POS,"local_Ndip"); - printf("%i : %i %i %i %zu %zu \n",ringid,local_z0,local_z1_coer,local_z1,local_Ndip,local_Nx); + D("%i : %i %i %i %zu %zu \n",ringid,local_z0,local_z1_coer,local_z1,local_Ndip,local_Nx); } //============================================================ diff --git a/src/cpp/fft_setup.cpp b/src/cpp/fft_setup.cpp index 57afe8e1..d623ea88 100644 --- a/src/cpp/fft_setup.cpp +++ b/src/cpp/fft_setup.cpp @@ -74,7 +74,7 @@ using namespace std; extern void getKernelWorkDimensions(cl_fft_plan *plan, cl_fft_kernel_info *kernelInfo, cl_int *batchSize, size_t *gWorkItems, size_t *lWorkItems); -extern const char coptions[]; +extern const char *coptions; static void getBlockConfigAndKernelString(cl_fft_plan *plan) diff --git a/src/iterative.c b/src/iterative.c index f525e2ca..2f75f075 100644 --- a/src/iterative.c +++ b/src/iterative.c @@ -50,6 +50,9 @@ extern doublecomplex * restrict vec1,* restrict vec2,* restrict vec3,* restrict extern const double iter_eps; extern const enum init_field InitField; extern const bool recalc_resid; +extern const time_t chp_time; +extern const char *chp_dir; + // defined and initialized in timing.c extern TIME_TYPE Timing_OneIter,Timing_OneIterComm,Timing_InitIter,Timing_InitIterComm, Timing_IntFieldOneComm; diff --git a/src/make_particle.c b/src/make_particle.c index 57c7f9e1..56a0433a 100644 --- a/src/make_particle.c +++ b/src/make_particle.c @@ -47,9 +47,9 @@ extern const enum sym sym_type; extern const double lambda; extern double sizeX,dpl,a_eq; extern const int jagged; -extern const char shape_fname[]; -extern char shapename[]; -extern char save_geom_fname[]; +extern const char *shape_fname; +extern const char *shapename; +extern const char *save_geom_fname; extern const bool volcor,save_geom; extern opt_index opt_sh; extern const double gr_vf; @@ -154,12 +154,16 @@ static void SaveGeometry(void) // create save_geom_fname if not specified, by adding extension to the shapename if (save_geom_fname[0]==0) { - if (sg_format==SF_TEXT || sg_format==SF_TEXT_EXT) - sprintf(save_geom_fname,"%s.geom",shapename); - else if (sg_format==SF_DDSCAT6 || sg_format==SF_DDSCAT7) - sprintf(save_geom_fname,"%s.dat",shapename); + const char *ext; + char *buf; // actual storage for save_geom_fname + // choose extension + if (sg_format==SF_TEXT || sg_format==SF_TEXT_EXT) ext="geom"; + else if (sg_format==SF_DDSCAT6 || sg_format==SF_DDSCAT7) ext="dat"; + else LogError(ONE_POS,"Unknown format for saved geometry file (%d)",sg_format); + MALLOC_VECTOR(buf,char,strlen(shapename)+strlen(ext)+2,ALL); + sprintf(buf,"%s.%s",shapename,ext); + save_geom_fname=buf; } - // automatically change format if needed if (sg_format==SF_TEXT && Nmat>1) sg_format=SF_TEXT_EXT; // choose filename @@ -179,16 +183,18 @@ static void SaveGeometry(void) "#box size: %dx%dx%d\n",shapename,boxX,boxY,boxZ); if (sg_format==SF_TEXT_EXT) fprintf(geom,"Nmat=%d\n",Nmat); } - else if (sg_format==SF_DDSCAT6 || sg_format==SF_DDSCAT7) + else if (sg_format==SF_DDSCAT6 || sg_format==SF_DDSCAT7) { fprintf(geom,"shape: '%s'; box size: %dx%dx%d; generated by ADDA v."ADDA_VERSION"\n" "%zu = NAT\n" "1 0 0 = A_1 vector\n" "0 1 0 = A_2 vector\n" - "1 1 1 = lattice spacings (d_x,d_y,d_z)/d\n" - "JA IX IY IZ ICOMP(x,y,z)\n",shapename,boxX,boxY,boxZ,nvoid_Ndip); + "1 1 1 = lattice spacings (d_x,d_y,d_z)/d\n", + shapename,boxX,boxY,boxZ,nvoid_Ndip); if (sg_format==SF_DDSCAT7) fprintf(geom, "%g %g %g = coordinates (x0,y0,z0)/d of the zero dipole (IX=IY=IZ=0)\n", (1-boxX)/2.0,(1-boxY)/2.0,(1-boxZ)/2.0); + fprintf(geom,"JA IX IY IZ ICOMP(x,y,z)\n"); + } #ifdef PARALLEL } // end of if #endif @@ -397,7 +403,6 @@ static void InitDipFile(const char * restrict fname,int *bX,int *bY,int *bZ,int */ fseek(dipfile,0,SEEK_SET); SkipNLines(dipfile,skiplines); - } //=========================================================== @@ -1769,7 +1774,11 @@ void InitShape(void) PrintError("Specified domain number to be granulated (%d) is larger than total number " "of domains (%d) for the given shape (%s)",gr_mat+1,Nmat_need,shapename); else Nmat_need++; - strcat(shapename,"_gran"); + // update shapename; use new storage + char *buf; + MALLOC_VECTOR(buf,char,strlen(shapename)+6,ALL); + sprintf(buf,"%s_gran",shapename); + shapename=buf; } // check if enough refractive indices or extra if (Nmat=size) PrintErrorHelp("Too long argument to '-%s' option (only %ud chars allowed). If you really " "need it you may increase MAX_DIRNAME in const.h and recompile",OptionName(),size-1); - strcpy(dest,str); + return str; } //============================================================ @@ -767,8 +770,8 @@ INLINE void ScanIntError(const char * restrict str,int *res) //============================================================ -INLINE bool ScanFnamesError(const int Narg,const int need,char **argv,char * restrict fname1, - char * restrict fname2) +INLINE bool ScanFnamesError(const int Narg,const int need,char **argv,const char **fname1, + const char **fname2) /* If 'need' corresponds to one of FNAME_ARG, scan Narg<=2 filenames from argv into fname1 and * fname2. All consistency checks are left to the caller (in particular, whether Narg corresponds * to need). argv should be shifted to contain only filenames. fname2 can be NULL, but it will @@ -780,11 +783,11 @@ INLINE bool ScanFnamesError(const int Narg,const int need,char **argv,char * res bool res=false; if (IS_FNAME_ARG(need)) { - ScanStrError(argv[0],MAX_FNAME,fname1); + *fname1=ScanStrError(argv[0],MAX_FNAME); if (Narg==2) { if (fname2==NULL) // consistency check e.g. for reading shape filename PrintError("Failed to store the second filename in function ScanFnamesError"); - else ScanStrError(argv[1],MAX_FNAME,fname2); + else *fname2=ScanStrError(argv[1],MAX_FNAME); } res=true; } @@ -859,7 +862,7 @@ static void PrintTime(char * restrict s,const time_t *time_ptr) PARSE_FUNC(alldir_inp) { - ScanStrError(argv[1],MAX_FNAME,alldir_parms); + alldir_parms=ScanStrError(argv[1],MAX_FNAME); } PARSE_FUNC(anisotr) { @@ -899,7 +902,7 @@ PARSE_FUNC(beam) * NargError function similarly as done in existing tests. */ // either parse filename or parse all parameters as float; consistency is checked later - if (!ScanFnamesError(Narg,need,argv+2,beam_fnameY,beam_fnameX)) + if (!ScanFnamesError(Narg,need,argv+2,&beam_fnameY,&beam_fnameX)) for (j=0;j2) PrintErrorHelp( "Illegal number of arguments (%d) to '-orient avg' option (0 or 1 expected)",Narg-1); orient_avg=true; - if (Narg==2) ScanStrError(argv[2],MAX_FNAME,avg_parms); + if (Narg==2) avg_parms=ScanStrError(argv[2],MAX_FNAME); } else { if (Narg!=3) NargError(Narg,"3"); @@ -1192,7 +1195,7 @@ PARSE_FUNC(pol) PARSE_FUNC(prognosis) { prognosis=true; - strcpy(run_name,"test"); + run_name="test"; } PARSE_FUNC(prop) { @@ -1216,7 +1219,7 @@ PARSE_FUNC(save_geom) { if (Narg>1) NargError(Narg,"0 or 1"); save_geom=true; - if (Narg==1) ScanStrError(argv[1],MAX_FNAME,save_geom_fname); + if (Narg==1) save_geom_fname=ScanStrError(argv[1],MAX_FNAME); } PARSE_FUNC(scat) { @@ -1228,7 +1231,7 @@ PARSE_FUNC(scat) } PARSE_FUNC(scat_grid_inp) { - ScanStrError(argv[1],MAX_FNAME,scat_grid_parms); + scat_grid_parms=ScanStrError(argv[1],MAX_FNAME); } PARSE_FUNC(scat_matr) { @@ -1290,7 +1293,7 @@ PARSE_FUNC(shape) * NargError function similarly as done in existing tests. */ // either parse filename or parse all parameters as float; consistency is checked later - if (!ScanFnamesError(Narg,need,argv+2,shape_fname,NULL)) + if (!ScanFnamesError(Narg,need,argv+2,&shape_fname,NULL)) for (j=0;j