9.0
general documentation
Loading...
Searching...
No Matches
cs_file.cpp File Reference
#include "base/cs_defs.h"
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bft/bft_error.h"
#include "bft/bft_printf.h"
#include "base/cs_log.h"
#include "base/cs_mem.h"
#include "base/cs_file.h"
Include dependency graph for cs_file.cpp:

Functions

cs_file_tcs_file_open (const char *name, cs_file_mode_t mode, cs_file_access_t method)
 Create a file descriptor and open the associated file.
cs_file_tcs_file_open_default (const char *name, cs_file_mode_t mode)
 Create a file descriptor and open the associated file, using the default file communicator and access method.
cs_file_tcs_file_open_serial (const char *name, cs_file_mode_t mode)
 Create a file descriptor and open the associated file, using the serial IO on the root rank.
cs_file_tcs_file_free (cs_file_t *f)
 Destroy a file descriptor and close the associated file.
void * cs_file_in_memory_get_data (cs_file_t *f)
 Get pointer to data for file in memory.
void cs_file_set_allow_read_global_eof (cs_file_t *f, bool allow_eof)
void cs_file_in_memory_transfer_data (cs_file_t *f, size_t nb, void *data)
 Transfer a block of data to file in memory.
const char * cs_file_get_name (const cs_file_t *f)
 Return a file's name.
void cs_file_set_big_endian (cs_file_t *f)
 Ensure that data is read or written in big-endian (network standard) format.
int cs_file_get_swap_endian (const cs_file_t *f)
 Return a file's byte-swapping behavior.
void cs_file_set_swap_endian (cs_file_t *f, int swap)
 Set a file's byte-swapping behavior.
size_t cs_file_read_global (cs_file_t *f, void *buf, size_t size, size_t ni)
 Read global data from a file, distributing it to all processes associated with that file.
size_t cs_file_write_global (cs_file_t *f, const void *buf, size_t size, size_t ni)
 Write global data to a file.
size_t cs_file_read_block (cs_file_t *f, void *buf, size_t size, size_t stride, cs_gnum_t global_num_start, cs_gnum_t global_num_end)
 Read data to a buffer, distributing a contiguous part of it to each process associated with a file.
size_t cs_file_write_block (cs_file_t *f, const void *buf, size_t size, size_t stride, cs_gnum_t global_num_start, cs_gnum_t global_num_end)
 Write data to a file, each associated process providing a contiguous part of this data.
size_t cs_file_write_block_buffer (cs_file_t *f, void *buf, size_t size, size_t stride, cs_gnum_t global_num_start, cs_gnum_t global_num_end)
 Write data to a file, each associated process providing a contiguous part of this data.
int cs_file_seek (cs_file_t *f, cs_file_off_t offset, cs_file_seek_t whence)
 Update the file pointer according to whence.
cs_file_off_t cs_file_tell (cs_file_t *f)
 Return the position of the file pointer.
char * cs_file_gets (char *s, const int size, const cs_file_t *f, int *line)
 Formatted input from a text file (as fgets()).
char * cs_file_gets_try (char *s, const int size, const cs_file_t *f, int *line)
 Formatted input from a text file if possible (as fgets()).
void cs_file_dump (const cs_file_t *f)
 Dump the metadata of a file structure in human readable form.
void cs_file_free_defaults (void)
 Free the default options for file access.
void cs_file_get_default_access (cs_file_mode_t mode, cs_file_access_t *method)
 Get the default options for file access.
void cs_file_set_default_access (cs_file_mode_t mode, cs_file_access_t method)
 Set the default options for file access.
cs_file_mpi_positioning_t cs_file_get_mpi_io_positioning (void)
 Get the positioning method for MPI-IO.
void cs_file_set_mpi_io_positioning (cs_file_mpi_positioning_t positioning)
 Set the positioning method for MPI-IO.
void cs_file_defaults_info (void)
 Print information on default options for file access.
int cs_file_mkdir_default (const char *path)
 Create a new directory using default permissions.
int cs_file_isreg (const char *path)
 Check if a file exists and is a regular file.
int cs_file_isdir (const char *path)
 Check if a directory exists.
char ** cs_file_listdir (const char *path)
 List files inside a directory.
cs_file_off_t cs_file_size (const char *path)
 Return the size of a file.
int cs_file_remove (const char *path)
 Remove a file if it exists and is a regular file or an empty directory.
int cs_file_endswith (const char *path, const char *end)
 Check if file name/path ends with a specific string.

Variables

const char * cs_file_access_name []

Detailed Description

File and directory operations, with parallel IO.

Function Documentation

◆ cs_file_defaults_info()

void cs_file_defaults_info ( void )

Print information on default options for file access.

◆ cs_file_dump()

void cs_file_dump ( const cs_file_t * f)

Dump the metadata of a file structure in human readable form.

Parameters
[in]fcs_file_t descriptor

◆ cs_file_endswith()

int cs_file_endswith ( const char * path,
const char * end )

Check if file name/path ends with a specific string.

The function returns an int: 1 if the file name ends with the given string, 0 otherwise.

Parameters
[in]pathname of file
[in]endend string to test
Returns
1 if the path ends with the given string, 0 otherwise.

◆ cs_file_free()

cs_file_t * cs_file_free ( cs_file_t * f)

Destroy a file descriptor and close the associated file.

Parameters
[in,out]ffile descriptor to destroy

◆ cs_file_free_defaults()

void cs_file_free_defaults ( void )

Free the default options for file access.

◆ cs_file_get_default_access()

void cs_file_get_default_access ( cs_file_mode_t mode,
cs_file_access_t * method )

Get the default options for file access.

Parameters
[in]modefile mode for which the default is queried (write and append use the same method, and are interchangeable here)
[out]methoddefault file access method, or nullptr

◆ cs_file_get_mpi_io_positioning()

cs_file_mpi_positioning_t cs_file_get_mpi_io_positioning ( void )

Get the positioning method for MPI-IO.

For details, see cs_file_set_mpi_io_positioning.

Returns
positioning method for MPI-IO

◆ cs_file_get_name()

const char * cs_file_get_name ( const cs_file_t * f)

Return a file's name.

Parameters
[in]fcs_file_t descriptor
Returns
pointer to the file's name.

◆ cs_file_get_swap_endian()

int cs_file_get_swap_endian ( const cs_file_t * f)

Return a file's byte-swapping behavior.

Parameters
[in]fcs_file_t descriptor
Returns
0 if file's endianness is the same as the system's, 1 otherwise.

◆ cs_file_gets()

char * cs_file_gets ( char * s,
const int size,
const cs_file_t * f,
int * line )

Formatted input from a text file (as fgets()).

Parameters
[out]sbuffer to which string is to be read.
[in]sizemaximum number of characters to be read plus one.
[in]fecs_file_t descriptor.
[in,out]linefile line number if available, or nullptr.
Returns
s on success, nullptr on error or when end of file occurs and no characters have been read.

◆ cs_file_gets_try()

char * cs_file_gets_try ( char * s,
const int size,
const cs_file_t * f,
int * line )

Formatted input from a text file if possible (as fgets()).

This function is similar to cs_file_gets(), but failure to read a line due to an end-of-file condition is not considered an error with this variant, which may be used to read text files or sections thereof of unknown length.

Parameters
[out]sbuffer to which string is to be read.
[in]sizemaximum number of characters to be read plus one.
[in]fcs_file_t descriptor.
[in,out]linefile line number if available, or nullptr.
Returns
s on success, nullptr on error or when end of file occurs and no characters have been read.

◆ cs_file_in_memory_get_data()

void * cs_file_in_memory_get_data ( cs_file_t * f)

Get pointer to data for file in memory.

Parameters
[in]fcs_file_t descriptor
Returns
pointer to file data.

◆ cs_file_in_memory_transfer_data()

void cs_file_in_memory_transfer_data ( cs_file_t * f,
size_t nb,
void * data )

Transfer a block of data to file in memory.

Only for files opened using CS_FILE_IN_MEMORY_SERIAL.

Parameters
[in]fcs_file_t descriptor
[in]nbnumber of matching bytes for data
[in]datadata buffer (ownership is relinquished by caller)

◆ cs_file_isdir()

int cs_file_isdir ( const char * path)

Check if a directory exists.

Parameters
[in]pathdirectory path.
Returns
1 if directory exists, 0 otherwise.

◆ cs_file_isreg()

int cs_file_isreg ( const char * path)

Check if a file exists and is a regular file.

Parameters
[in]pathfile path.
Returns
1 if file exists and is a regular file, 0 otherwise.

◆ cs_file_listdir()

char ** cs_file_listdir ( const char * path)

List files inside a directory.

The array returned must be freed by the caller using CS_FREE, as well as the individual entries in the array.

Parameters
[in]pathname of directory.
Returns
an array of file names in a directory. The last entry is set to nullptr. If no means to list the directory or an error occured, the return value is simply nullptr.

◆ cs_file_mkdir_default()

int cs_file_mkdir_default ( const char * path)

Create a new directory using default permissions.

This function is similar to the POSIX function mkdir(), except that it has no "mode" argument: by default, on a POSIX type system, permissions include read, write, and execute access for the user, group and others, modified by the users umask value (so with a typical configuration, the user will have read, write, and execute pemission, the group and others will only have read and execute permission, but this behavior may be modified).

Also, contrary to the usual mkdir(), if the directory already exists (and is truly a directory), this is considered a success and not a failure, and 0 is returned: the aim of this function is to make a directory available, so if it already exists, this is considered acceptable.

Parameters
[in]pathname of new directory.
Returns
0 on success, -1 if an error occured (in which case errno contains the appropriate error code). If the underlying system has no mkdir() function or it was not detected upon BFT configuration, 1 is returned.

◆ cs_file_open()

cs_file_t * cs_file_open ( const char * name,
cs_file_mode_t mode,
cs_file_access_t method )

Create a file descriptor and open the associated file.

By default, data is written or read as native data. This behavior may be modified by cs_file_set_swap_endian().

Parameters
[in]namefile name
[in]modefile access mode: read, write, or append
[in]methodfile access method (currently only C standard-IO when built without MPI)
[in]hintsassociated hints for MPI-IO, or MPI_INFO_NULL
[in]block_commhandle to MPI communicator used for distributed file block access (may be a subset of comm if some ranks do not directly access distributed data blocks)
[in]commhandle to main MPI communicator
Returns
pointer to cs_file_t file descriptor (nullptr in case of failure); currently, errors are fatal.

◆ cs_file_open_default()

cs_file_t * cs_file_open_default ( const char * name,
cs_file_mode_t mode )

Create a file descriptor and open the associated file, using the default file communicator and access method.

By default, data is written or read as native data. This behavior may be modified by cs_file_set_swap_endian().

Parameters
[in]namefile name
[in]modefile access mode: read, write, or append
Returns
pointer to cs_file_t file descriptor (nullptr in case of failure); currently, errors are fatal.

◆ cs_file_open_serial()

cs_file_t * cs_file_open_serial ( const char * name,
cs_file_mode_t mode )

Create a file descriptor and open the associated file, using the serial IO on the root rank.

By default, data is written or read as native data. This behavior may be modified by cs_file_set_swap_endian().

Parameters
[in]namefile name
[in]modefile access mode: read, write, or append
Returns
pointer to cs_file_t file descriptor (nullptr in case of failure); currently, errors are fatal.

◆ cs_file_read_block()

size_t cs_file_read_block ( cs_file_t * f,
void * buf,
size_t size,
size_t stride,
cs_gnum_t global_num_start,
cs_gnum_t global_num_end )

Read data to a buffer, distributing a contiguous part of it to each process associated with a file.

Each process should receive a (possibly empty) block of the data, and we should have: global_num_start at rank 0 = 1 global_num_start at rank i+1 = global_num_end at rank i. Otherwise, behavior (especially positioning for future reads) is undefined.

Parameters
[in]fcs_file_t descriptor
[out]bufpointer to location receiving data
[in]sizesize of each item of data in bytes
[in]stridenumber of (interlaced) values per block item
[in]global_num_startglobal number of first block item (1 to n numbering)
[in]global_num_endglobal number of past-the end block item (1 to n numbering)
Returns
the (local) number of items (not bytes) sucessfully read; currently, errors are fatal.

◆ cs_file_read_global()

size_t cs_file_read_global ( cs_file_t * f,
void * buf,
size_t size,
size_t ni )

Read global data from a file, distributing it to all processes associated with that file.

Parameters
[in]fcs_file_t descriptor
[out]bufpointer to location receiving data
[in]sizesize of each item of data in bytes
[in]ninumber of items to read
Returns
the number of items (not bytes) sucessfully read; currently, errors are fatal.

◆ cs_file_remove()

int cs_file_remove ( const char * path)

Remove a file if it exists and is a regular file or an empty directory.

Parameters
[in]pathfile path.
Returns
0 in case of success or if file does not exist, not 0 otherwise.

◆ cs_file_seek()

int cs_file_seek ( cs_file_t * f,
cs_file_off_t offset,
cs_file_seek_t whence )

Update the file pointer according to whence.

Parameters
[in,out]fcs_file_t descriptor
[in]offsetadd to position specified to whence to obtain new position, measured in characters from the beginning of the file
[in]whencebeginning if CS_FILE_SEEK_SET, current if CS_FILE_SEEK_CUR, or end-of-file if CS_FILE_SEEK_END
Returns
0 upon success, nonzero otherwise; currently, errors are fatal.

◆ cs_file_set_allow_read_global_eof()

void cs_file_set_allow_read_global_eof ( cs_file_t * f,
bool allow_eof )

◆ cs_file_set_big_endian()

void cs_file_set_big_endian ( cs_file_t * f)

Ensure that data is read or written in big-endian (network standard) format.

Parameters
[in,out]fcs_file_t descriptor

◆ cs_file_set_default_access()

void cs_file_set_default_access ( cs_file_mode_t mode,
cs_file_access_t method )

Set the default options for file access.

If the method given contains incompatible values, such as when setting MPI-IO methods when MPI-IO is not available, a "reasonable" default is used instead.

Parameters
[in]modefile mode for which the default is being set (write and append use the same method, and are interchangeable here)
[in]methoddefault access method to set

◆ cs_file_set_mpi_io_positioning()

void cs_file_set_mpi_io_positioning ( cs_file_mpi_positioning_t positioning)

Set the positioning method for MPI-IO.

It is not always known whether a performance or robustness difference is to be expected using explicit file offsets or individual file pointers. Perusal of a sampling of ROMIO code would seem to indicate that no difference is to be expected, but this might change with MPI IO variants or file systems, so this advanced setting is made possible.

This setting is not available on a per-file basis, though this could be done in the future in the unexpected case of performance results showing this would be useful.

Parameters
[in]positioningchosen positioning method for MPI-IO

◆ cs_file_set_swap_endian()

void cs_file_set_swap_endian ( cs_file_t * f,
int swap )

Set a file's byte-swapping behavior.

Parameters
[in,out]fcs_file_t descriptor
[in]swap1 if bytes must be swapped, 0 otherwise

◆ cs_file_size()

cs_file_off_t cs_file_size ( const char * path)

Return the size of a file.

If the file does not exist, 0 is returned.

Note also that for some special files, such as files in the Linux /proc directory, this may return 0.

Parameters
[in]pathfile path.
Returns
size of file.

◆ cs_file_tell()

cs_file_off_t cs_file_tell ( cs_file_t * f)

Return the position of the file pointer.

In parallel, we consider the file pointer to be equal to the highest value of the individual file pointers.

Parameters
[in]fcs_file_t descriptor
Returns
current position of the file pointer.

◆ cs_file_write_block()

size_t cs_file_write_block ( cs_file_t * f,
const void * buf,
size_t size,
size_t stride,
cs_gnum_t global_num_start,
cs_gnum_t global_num_end )

Write data to a file, each associated process providing a contiguous part of this data.

Each process should provide a (possibly empty) block of the data, and we should have: global_num_start at rank 0 = 1 global_num_start at rank i+1 = global_num_end at rank i. Otherwise, behavior (especially positioning for future reads) is undefined.

This function may require an internal copy of the data to ensure that the buffer contents are not modified, so if the buffer contents are temporary values, to be deleted after writing, using cs_file_write_block_buffer() instead may be used to avoid an unneeded memory allocation and copy.

Parameters
[in]fcs_file_t descriptor
[in]bufpointer to location containing data
[in]sizesize of each item of data in bytes
[in]stridenumber of (interlaced) values per block item
[in]global_num_startglobal number of first block item (1 to n numbering)
[in]global_num_endglobal number of past-the end block item (1 to n numbering)
Returns
the (local) number of items (not bytes) sucessfully written; currently, errors are fatal.

◆ cs_file_write_block_buffer()

size_t cs_file_write_block_buffer ( cs_file_t * f,
void * buf,
size_t size,
size_t stride,
cs_gnum_t global_num_start,
cs_gnum_t global_num_end )

Write data to a file, each associated process providing a contiguous part of this data.

Each process should provide a (possibly empty) block of the data, and we should have: global_num_start at rank 0 = 1 global_num_start at rank i+1 = global_num_end at rank i. Otherwise, behavior (especially positioning for future reads) is undefined.

This function is intended to be used mainly data that is already a copy of original data (such as data that has been redistributed across processors just for the sake of output), or that is to be deleted after writing, so it may modify the values in its input buffer (notably to convert from little-endian to big-endian of vice-versa if necessary).

Parameters
[in]fcs_file_t descriptor
[in,out]bufpointer to location containing data
[in]sizesize of each item of data in bytes
[in]stridenumber of (interlaced) values per block item
[in]global_num_startglobal number of first block item (1 to n numbering)
[in]global_num_endglobal number of past-the end block item (1 to n numbering)
Returns
the (local) number of items (not bytes) sucessfully written; currently, errors are fatal.

◆ cs_file_write_global()

size_t cs_file_write_global ( cs_file_t * f,
const void * buf,
size_t size,
size_t ni )

Write global data to a file.

Under MPI, data is only written by the associated communicator's root rank. The buffers on other ranks are ignored, though the file offset is updated (i.e. the call to this function is collective).

Parameters
[in]fcs_file_t descriptor
[in]bufpointer to location containing data
[in]sizesize of each item of data in bytes
[in]ninumber of items to write
Returns
the number of items (not bytes) sucessfully written; currently, errors are fatal.

Variable Documentation

◆ cs_file_access_name

const char* cs_file_access_name[]
Initial value:
= {N_("default"),
N_("standard input and output, serial access"),
N_("standard input and output, parallel access"),
N_("non-collective MPI-IO, independent file open/close"),
N_("non-collective MPI-IO, collective file open/close"),
N_("collective MPI-IO")}
#define N_(String)
Definition cs_defs.h:68