29 #include "petsc_utils.h" 39 memcpy( (
char*)sl->
data+sl->
nitems*quantum, p, quantum );
45 if (!old_str)
return NULL;
47 size_t len = strlen(old_str);
48 char *new_str = (
char *)calloc(len+1,
sizeof(
char));
49 strcpy(new_str, old_str);
56 char *rs = (
char *)malloc(256);
57 snprintf(rs, 255,
"%.1f", r);
64 size_t i = path.rfind(sep, path.length());
65 if (i != std::string::npos) {
66 return path.substr(i+1, path.length() - i);
82 snprintf(fmsg,
sizeof fmsg,
"L%d ", level );
84 snprintf(fmsg+strlen(fmsg),
sizeof fmsg,
"[P%d] ",
get_rank() );
85 if(level || (flag&LOCAL) || (flag&SYNCED))
91 vsnprintf(fmsg+strlen(fmsg),
MAX_MESG_LEN-strlen(fmsg)-1, fmt, ap);
98 if(flag&SYNCED && out->
fd)
99 FPRINTF_SYNC(WORLD out->
fd,
"%s", fmsg);
100 else if(!
get_rank() || (flag&LOCAL)) {
101 fprintf(out->
fd,
"%s", fmsg);
106 if(out && flag &
FLUSH) {
107 if(flag&SYNCED && out->
fd)
108 PRINTF_FLUSH(COMM_W, out->
fd);
114 if((flag&
ECHO) && fmt) {
116 FPRINTF_SYNC(WORLD level ? stderr : stdout,
"%s", fmsg);
117 else if(!
get_rank() || (flag&LOCAL))
118 fprintf(level ? stderr : stdout,
"%s", fmsg);
122 if((flag&ECHO) && ((flag & FLUSH) || level ==
MAX_LOG_LEVEL)) {
124 PRINTF_FLUSH(COMM_W, level ? stderr : stdout);
126 fflush(level ? stderr : stdout);
132 return access(fname, F_OK) != -1;
140 bool openfail =
false;
143 f->
fd = fopen(fname,mode);
150 char current_workdir[2048]; getcwd(current_workdir, 2048);
152 fprintf(stderr,
"Error, failed to open file: \"%s/%s\"!\n", current_workdir, fname);
153 fprintf(stderr,
"%s\n", strerror(error));
174 fread(ptr, size, nmemb, stream->
fd);
176 MPI_Bcast(ptr, size*nmemb, MPI_BYTE, 0, comm);
184 if(rank == source_pid) {
185 MPI_Send(&nmemb,
sizeof(
size_t), MPI_BYTE, 0, 100, comm);
186 MPI_Send(ptr, nmemb*size, MPI_BYTE, 0, 100, comm);
192 MPI_Recv(&nmemb,
sizeof(
size_t), MPI_BYTE, source_pid, 100, comm, &status);
194 MPI_Recv(wbuff.
data(), size*nmemb, MPI_BYTE, source_pid, 100, comm, &status);
195 fwrite(wbuff.
data(), size, nmemb, stream->
fd);
208 fgets(s, size, stream->
fd);
211 MPI_Bcast(s, size, MPI_CHAR, 0, comm);
213 int str_len = strlen(s);
215 if(str_len)
return s;
224 fwrite( &len,
sizeof(
int), 1, out->
fd);
225 fwrite( s,
sizeof(
char), len, out->
fd);
231 fread( &len,
sizeof(
int), 1, in->
fd );
232 char *s = (
char*)malloc( len+1 );
233 fread( s,
sizeof(
char), len, in->
fd );
243 char *s = (
char*) malloc(len+1);
254 bool inside_x = p.
x >= shape.
p0.
x && p.
x <= shape.
p1.
x;
255 bool inside_y = p.
y >= shape.
p0.
y && p.
y <= shape.
p1.
y;
256 bool inside_z = p.
z >= shape.
p0.
z && p.
z <= shape.
p1.
z;
257 return (inside_x && inside_y && inside_z);
266 Point center_to_point = p - shape.
p0;
268 double h =
mag(height);
271 double height_projection =
dot(height, center_to_point);
273 if(height_projection >= 0 && height_projection <= h) {
274 center_to_point -= (height * height_projection);
293 char *p = (
char *)&i;
307 FILE* fd = fopen(file,
"r");
322 if(strlen(path) && path[0] ==
'/')
return true;
328 #ifdef USE_FMEM_WRAPPER 336 static fpos_t SeekFn(
void *handler, fpos_t offset,
int whence) {
338 fmem_t *mem = (fmem_t*)handler;
342 if (offset > 0 && (
size_t)offset <= mem->size)
343 return mem->pos = offset;
346 if (mem->pos + offset <= mem->size)
347 return mem->pos = offset;
351 if (mem->size + offset <= mem->size)
352 return pos = mem->size + offset;
366 static int ReadFn(
void *handler,
char *buf,
int size) {
368 fmem_t *mem = (fmem_t*)handler;
369 size_t available = mem->size - mem->pos;
371 if (size < 0)
return - 1;
373 if ((
size_t)size > available)
376 while (count < (
size_t)size)
377 buf[count++] = mem->buffer[mem->pos++];
389 static int WriteFn(
void *handler,
const char *buf,
int size) {
391 fmem_t *mem = (fmem_t*)handler;
392 size_t available = mem->size - mem->pos;
394 if (size < 0)
return - 1;
396 if ((
size_t)size > available)
399 while (count < (
size_t)size)
400 mem->buffer[mem->pos++] = buf[count++];
410 static int CloseFn(
void *handler) {
423 FILE *
fmemopen_(
void *buf,
size_t size,
const char *mode) {
424 fmem_t *mem = (fmem_t *) malloc(
sizeof(fmem_t));
426 memset(mem, 0,
sizeof(fmem_t));
427 mem->size = size, mem->buffer = (
char*)buf;
429 return funopen(mem, ReadFn, WriteFn, SeekFn, CloseFn);
432 #endif // USE_FMEM_WRAPPER void sltlst_append(Salt_list *sl, void *p, int quantum)
class to store shape definitions
char * dupstr(const char *old_str)
void f_read_par(void *ptr, size_t size, size_t nmemb, FILE_SPEC stream, MPI_Comm comm)
Parallel fread. Root reads, then broadcasts.
void count(const vector< T > &data, vector< S > &cnt)
Count number of occurrences of indices.
T * data()
Pointer to the vector's start.
void write_bin_string(FILE_SPEC out, const char *s)
char * read_bin_string_par(FILE_SPEC in)
V mag2(const vec3< V > &vect)
V mag(const vec3< V > &vect)
char * read_bin_string(FILE_SPEC in)
int chunk
allocate memory to hold this many items
bool file_can_be_opened(const char *file)
Check wheterh a file can be opened for reading.
T get_global(T in, MPI_Op OP, MPI_Comm comm=PETSC_COMM_WORLD)
Do a global reduction on a variable.
V dot(const vec3< V > &p1, const vec3< V > &p2)
bool f_exist(const char *fname)
std::string get_basename(const std::string &path)
void log_msg(FILE_SPEC out, int level, unsigned char flag, const char *fmt,...)
int nitems
number of items
A vector storing arbitrary data.
void f_close(FILE_SPEC &f)
Close a FILE_SPEC.
saltatory list – memory is allocated in chunks
Basic utility structs and functions, mostly IO related.
bool point_in_shape(const Point &p, const geom_shape &shape)
test if a point is inside a simple geometric shape
V dist_2(const vec3< V > &p1, const vec3< V > &p2)
char * f_gets_par(char *s, int size, FILE_SPEC stream, MPI_Comm comm)
int size
size of list items
int get_rank(MPI_Comm comm=PETSC_COMM_WORLD)
FILE_SPEC f_open(const char *fname, const char *mode)
Open a FILE_SPEC.
bool path_is_absolute(const char *path)
check whether path is absolute
void f_write_par(void *ptr, size_t size, size_t nmemb, int source_pid, FILE_SPEC stream, MPI_Comm comm)
Write in parallel. Data comes from one rank, rank 0 writes.
char * stringify(double r)