Skip to content

Commit

Permalink
add -l options to h5fuse
Browse files Browse the repository at this point in the history
  • Loading branch information
brtnfld committed Feb 6, 2024
1 parent 0843a8c commit b67917f
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 11 deletions.
109 changes: 109 additions & 0 deletions src/H5FDsubfiling/H5FDsubfiling.c
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,115 @@ H5Pget_fapl_subfiling(hid_t fapl_id, H5FD_subfiling_config_t *config_out)
H5_SUBFILING_FUNC_LEAVE_API;
} /* end H5Pget_fapl_subfiling() */

herr_t
H5FDsubfiling_get_file_mapping(hid_t file_id, char ***filenames, size_t *len)
{
subfiling_context_t *sf_context = NULL;
H5FD_t *driver = NULL;
H5F_t *file_ptr = NULL;
char **filenames_arr = NULL;
char *filepath = NULL;
char *subfile_dir = NULL;
char *base = NULL;
int num_subfiles = 0;
int num_digits = 0;
herr_t ret_value = SUCCEED;

if (file_id < 0)
H5_SUBFILING_GOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "invalid file ID");
if (!filenames)
H5_SUBFILING_GOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "`filenames` was NULL");
if (!len)
H5_SUBFILING_GOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "`len` was NULL");

*filenames = NULL;
*len = 0;

if (NULL == (file_ptr = H5VL_object(file_id)))
H5_SUBFILING_GOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "invalid file ID");

if (H5F_shared_get_file_driver(H5F_SHARED(file_ptr), &driver) < 0)
H5_SUBFILING_GOTO_ERROR(H5E_FILE, H5E_CANTGET, FAIL, "can't get driver structure from file ID");

if (NULL == (sf_context = H5_get_subfiling_object(((H5FD_subfiling_t *)driver)->context_id)))
H5_SUBFILING_GOTO_ERROR(H5E_FILE, H5E_CANTGET, FAIL, "can't get subfiling context from ID");

if (!sf_context->topology)
H5_SUBFILING_GOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "application topology hasn't been initialized yet for this file");

assert(sf_context->h5_file_id != UINT64_MAX);
assert(sf_context->h5_filename);
assert(sf_context->sf_num_subfiles > 0);
assert(sf_context->topology);

if (sf_context->topology->rank_is_ioc) {
assert(sf_context->sf_fids);
assert(sf_context->sf_num_fids > 0);

/* Get the basename of the full HDF5 filename */
if (H5_basename(sf_context->h5_filename, &base) < 0)
H5_SUBFILING_GOTO_ERROR(H5E_RESOURCE, H5E_CANTALLOC, FAIL, "can't get HDF5 file basename");

/*
* Get the directory prefix where subfiles will be placed.
* Under normal circumstances, the subfiles are co-located
* with the HDF5 file, but users may specify a different
* directory name.
*/
if (sf_context->subfile_prefix) {
if (NULL == (subfile_dir = H5MM_strdup(sf_context->subfile_prefix)))
H5_SUBFILING_GOTO_ERROR(H5E_RESOURCE, H5E_CANTALLOC, FAIL, "couldn't copy subfile prefix");
}
else {
if (H5_dirname(sf_context->h5_filename, &subfile_dir) < 0)
H5_SUBFILING_GOTO_ERROR(H5E_RESOURCE, H5E_CANTALLOC, FAIL, "couldn't get HDF5 file dirname");
}

num_subfiles = sf_context->sf_num_subfiles;
num_digits = (int)(log10(num_subfiles) + 1);

if (NULL == (filenames_arr = calloc(1, (size_t)sf_context->sf_num_fids * sizeof(char *))))
H5_SUBFILING_GOTO_ERROR(H5E_RESOURCE, H5E_CANTALLOC, FAIL, "couldn't allocate filenames array");

for (int i = 0; i < sf_context->sf_num_fids; i++) {
int subfile_idx;

if (NULL == (filepath = malloc(PATH_MAX)))
H5_SUBFILING_GOTO_ERROR(H5E_RESOURCE, H5E_CANTALLOC, FAIL,
"couldn't allocate space for subfile filename");

subfile_idx = (i * sf_context->topology->n_io_concentrators) + sf_context->topology->ioc_idx + 1;

snprintf(filepath, PATH_MAX, "%s/" H5FD_SUBFILING_FILENAME_TEMPLATE, subfile_dir, base,
sf_context->h5_file_id, num_digits, subfile_idx, num_subfiles);

filenames_arr[i] = filepath;
filepath = NULL;
}

*filenames = filenames_arr;
*len = (size_t)sf_context->sf_num_fids;
}

done:
if (ret_value < 0) {
if (filenames_arr) {
for (int i = 0; i < sf_context->sf_num_fids; i++) {
free(filenames_arr[i]);
}
}
free(filenames_arr);

*filenames = NULL;
*len = 0;
}

H5MM_free(base);
H5MM_free(subfile_dir);

H5_SUBFILING_FUNC_LEAVE_API;
}

static herr_t
H5FD__subfiling_get_default_config(hid_t fapl_id, H5FD_subfiling_config_t *config_out)
{
Expand Down
6 changes: 6 additions & 0 deletions src/H5FDsubfiling/H5FDsubfiling.h
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,12 @@ H5_DLL herr_t H5Pset_fapl_subfiling(hid_t fapl_id, const H5FD_subfiling_config_t
*/
H5_DLL herr_t H5Pget_fapl_subfiling(hid_t fapl_id, H5FD_subfiling_config_t *config_out);

/*
* The caller must call H5free_memory on each of the entries in `filenames`, as well
* as on `filenames` itself.
*/
H5_DLL herr_t H5FDsubfiling_get_file_mapping(hid_t file_id, char ***filenames, size_t *len);

#ifdef __cplusplus
}
#endif
Expand Down
21 changes: 21 additions & 0 deletions testpar/t_subfiling_vfd.c
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,27 @@ test_create_and_close(void)
file_id = H5Fcreate(SUBF_FILENAME, H5F_ACC_TRUNC, H5P_DEFAULT, fapl_id);
VRFY((file_id >= 0), "H5Fcreate succeeded");

char **filenames = NULL;
size_t len = 0;
H5FDsubfiling_get_file_mapping(file_id, &filenames, &len);

if (len > 0) {
int l_mpi_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &l_mpi_rank);
printf("I AM MPI RANK %d RESPONSIBLE FOR SUBFILES: [", l_mpi_rank);
for (size_t i = 0; i < len; i++) {
if (i > 0)
printf(", ");
printf("%s", filenames[i]);
}
printf("]\n\n");

for (size_t i = 0; i < len; i++) {
H5free_memory(filenames[i]);
}
H5free_memory(filenames);
}

VRFY((H5Fclose(file_id) >= 0), "File close succeeded");

H5E_BEGIN_TRY
Expand Down
58 changes: 47 additions & 11 deletions utils/subfiling_vfd/h5fuse.in
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ function usage {
configuration file either as a command-line argument or the script will
search for the *.config file in the current directory."
echo ""
echo "usage: h5fuse [-f filename] [-h] [-p] [-q] [-r] [-v] "
echo "usage: h5fuse [-f filename] [-h] [-p] [-q] [-r] [-v] [-l subfile1,subfile2,...]"
echo "-f filename Subfile configuration file."
echo "-h Print this help."
echo "-q Quiet all output. [no]"
echo "-l Comma seperated list of subfiles to process"

Check failure on line 33 in utils/subfiling_vfd/h5fuse.in

View workflow job for this annotation

GitHub Actions / Check for spelling errors

seperated ==> separated
echo "-p h5fuse is being run in parallel, with more than one rank. [no]"
echo "-r Remove subfiles after being processed. [no]"
echo "-v Verbose output. [no]"
Expand Down Expand Up @@ -172,7 +173,8 @@ verbose="false"
quiet="false"
rm_subf="false"
parallel="false"
while getopts "hpqrvf:" option; do
subf_list=()
while getopts "hpqrvf:l:" option; do
case $option in
f) # subfiling configuration file
file_config=$OPTARG;;
Expand All @@ -187,6 +189,9 @@ while getopts "hpqrvf:" option; do
rm_subf="true";;
v) # verbose output
verbose="true";;
l) # process a list of subfiles
# Split the comma-sperated list into an array
IFS=', ' read -r -a subf_list <<< "${OPTARG}";;
\?) # Invalid option
echo -e "$RED ERROR: Invalid option ${BLD}-${OPTARG}${RED} $NC"
usage
Expand Down Expand Up @@ -259,17 +264,48 @@ fi
nsubfiles=${#subfiles[@]}

# Get the number of local subfiles

subfiles_loc=()
subfiles_size=()
for i in "${subfiles[@]}"; do
subfile="${subfile_dir}/${i}"
if [ -f "${subfile}" ]; then
subfiles_loc+=("$subfile")
subfiles_size+=($(wc -c "${subfile}" | awk '{print $1}'))
else
subfiles_size+=(0)
fi
done

len=${#subf_list[@]}

if [[ "${len}" == "0" ]]; then

for i in "${subfiles[@]}"; do
subfile="${subfile_dir}/${i}"
if [ -f "${subfile}" ]; then
subfiles_loc+=("$subfile")
subfiles_size+=($(wc -c "${subfile}" | awk '{print $1}'))
else
subfiles_size+=(0)
fi
done

else

for ((i = 0; i < $nsubfiles; i++)); do
subfiles_size[i]=0
done

idx=0
for j in "${subf_list[@]}"; do
subfile1="${subfile_dir}/${j}"
for k in "${subfiles[@]}"; do
subfile="${subfile_dir}/${k}"
if [[ "${subfile1}" == "${subfile}" ]]; then
subfiles_loc+=("$subfile1")
subfiles_size[$idx]=$(wc -c "${subfile1}" | awk '{print $1}')
((idx++))
break
fi
done
if [[ "${idx}" == "${len}" ]]; then
break;
fi
done

fi

if [ "$quiet" == "false" ]; then
TIMEFORMAT="COMPLETION TIME = %R s"
Expand Down

0 comments on commit b67917f

Please sign in to comment.