22 memcpy( (
char*)sl->
data+sl->
nitems*quantum, p, quantum );
28 if (!old_str)
return NULL;
30 size_t len = strlen(old_str);
31 char *new_str = (
char *)calloc(len+1,
sizeof(
char));
32 strcpy(new_str, old_str);
39 char *rs = (
char *)malloc(256);
40 snprintf(rs, 255,
"%.1f", r);
47 size_t i = path.rfind(sep, path.length());
48 if (i != std::string::npos) {
49 return path.substr(i+1, path.length() - i);
63 const int rank = mpi_ready ?
get_rank() : 0;
68 snprintf(fmsg,
sizeof fmsg,
"L%d ", level );
70 snprintf(fmsg+strlen(fmsg),
sizeof fmsg,
"[P%d] ", rank );
77 vsnprintf(fmsg+strlen(fmsg),
MAX_MESG_LEN-strlen(fmsg)-1, fmt, ap);
85 fprintf(out->
fd,
"%s", fmsg);
90 if(out && flag &
FLUSH) {
95 if((flag&
ECHO) && fmt) {
97 fprintf(level ? stderr : stdout,
"%s", fmsg);
102 fflush(level ? stderr : stdout);
108 return access(fname, F_OK) != -1;
116 bool openfail =
false;
119 f->
fd = fopen(fname,mode);
126 char current_workdir[2048]; getcwd(current_workdir, 2048);
128 fprintf(stderr,
"Error, failed to open file: \"%s/%s\"!\n", current_workdir, fname);
129 fprintf(stderr,
"%s\n", strerror(error));
150 fread(ptr, size, nmemb, stream->
fd);
152 MPI_Bcast(ptr, size*nmemb, MPI_BYTE, 0, comm);
160 if(rank == source_pid) {
161 MPI_Send(&nmemb,
sizeof(
size_t), MPI_BYTE, 0, 100, comm);
162 MPI_Send(ptr, nmemb*size, MPI_BYTE, 0, 100, comm);
168 MPI_Recv(&nmemb,
sizeof(
size_t), MPI_BYTE, source_pid, 100, comm, &status);
170 MPI_Recv(wbuff.
data(), size*nmemb, MPI_BYTE, source_pid, 100, comm, &status);
171 fwrite(wbuff.
data(), size, nmemb, stream->
fd);
184 fgets(s, size, stream->
fd);
187 MPI_Bcast(s, size, MPI_CHAR, 0, comm);
189 int str_len = strlen(s);
191 if(str_len)
return s;
200 fwrite( &len,
sizeof(
int), 1, out->
fd);
201 fwrite( s,
sizeof(
char), len, out->
fd);
207 fread( &len,
sizeof(
int), 1, in->
fd );
208 char *s = (
char*)malloc( len+1 );
209 fread( s,
sizeof(
char), len, in->
fd );
219 char *s = (
char*) malloc(len+1);
230 bool inside_x = p.
x >= shape.
p0.
x && p.
x <= shape.
p1.
x;
231 bool inside_y = p.
y >= shape.
p0.
y && p.
y <= shape.
p1.
y;
232 bool inside_z = p.
z >= shape.
p0.
z && p.
z <= shape.
p1.
z;
233 return (inside_x && inside_y && inside_z);
242 Point center_to_point = p - shape.
p0;
244 double h =
mag(height);
247 double height_projection =
dot(height, center_to_point);
249 if(height_projection >= 0 && height_projection <= h) {
250 center_to_point -= (height * height_projection);
269 char *p = (
char *)&i;
283 FILE* fd = fopen(file,
"r");
298 if(strlen(path) && path[0] ==
'/')
return true;
304 #ifdef USE_FMEM_WRAPPER
312 static fpos_t SeekFn(
void *handler, fpos_t offset,
int whence) {
314 fmem_t *mem = (fmem_t*)handler;
318 if (offset > 0 && (
size_t)offset <= mem->size)
319 return mem->pos = offset;
322 if (mem->pos + offset <= mem->size)
323 return mem->pos = offset;
327 if (mem->size + offset <= mem->size)
328 return pos = mem->size + offset;
342 static int ReadFn(
void *handler,
char *buf,
int size) {
344 fmem_t *mem = (fmem_t*)handler;
345 size_t available = mem->size - mem->pos;
347 if (size < 0)
return - 1;
349 if ((
size_t)size > available)
352 while (
count < (
size_t)size)
353 buf[
count++] = mem->buffer[mem->pos++];
365 static int WriteFn(
void *handler,
const char *buf,
int size) {
367 fmem_t *mem = (fmem_t*)handler;
368 size_t available = mem->size - mem->pos;
370 if (size < 0)
return - 1;
372 if ((
size_t)size > available)
375 while (
count < (
size_t)size)
376 mem->buffer[mem->pos++] = buf[
count++];
386 static int CloseFn(
void *handler) {
399 FILE *
fmemopen_(
void *buf,
size_t size,
const char *mode) {
400 fmem_t *mem = (fmem_t *) malloc(
sizeof(fmem_t));
402 memset(mem, 0,
sizeof(fmem_t));
403 mem->size = size, mem->buffer = (
char*)buf;
405 return funopen(mem, ReadFn, WriteFn, SeekFn, CloseFn);
Basic utility structs and functions, mostly IO related.
A vector storing arbitrary data.
T * data()
Pointer to the vector's start.
class to store shape definitions
void count(const vector< T > &data, vector< S > &cnt)
Count number of occurrences of indices.
void sltlst_append(Salt_list *sl, void *p, int quantum)
char * f_gets_par(char *s, int size, FILE_SPEC stream, MPI_Comm comm)
V dot(const vec3< V > &p1, const vec3< V > &p2)
bool point_in_shape(const Point &p, const geom_shape &shape)
test if a point is inside a simple geometric shape
char * stringify(double r)
char * read_bin_string(FILE_SPEC in)
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=SF_COMM)
Do a global reduction on a variable.
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.
void write_bin_string(FILE_SPEC out, const char *s)
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
V mag(const vec3< V > &vect)
char * read_bin_string_par(FILE_SPEC in)
void f_read_par(void *ptr, size_t size, size_t nmemb, FILE_SPEC stream, MPI_Comm comm)
Parallel fread. Root reads, then broadcasts.
char * dupstr(const char *old_str)
V dist_2(const vec3< V > &p1, const vec3< V > &p2)
void log_msg(FILE_SPEC out, int level, unsigned char flag, const char *fmt,...)
int get_rank(MPI_Comm comm=SF_COMM)
bool f_exist(const char *fname)
std::string get_basename(const std::string &path)
void f_close(FILE_SPEC &f)
Close a FILE_SPEC.
V mag2(const vec3< V > &vect)
saltatory list – memory is allocated in chunks
int chunk
allocate memory to hold this many items
int size
size of list items
int nitems
number of items