Skip to content

Commit

Permalink
Merge pull request #1704 from willend/main
Browse files Browse the repository at this point in the history
Make use of mcpl_dump() in both McStas and McXtrace MCPL_output verbose mode
  • Loading branch information
willend authored Sep 20, 2024
2 parents 5590803 + 16824bb commit b32e8ea
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 35 deletions.
32 changes: 3 additions & 29 deletions mcstas-comps/misc/MCPL_output.comp
Original file line number Diff line number Diff line change
Expand Up @@ -65,32 +65,6 @@ int mcpl_file_exist (char *filename)
struct stat buffer;
return (stat (filename, &buffer) == 0);
}

int run_mcpltool(char *filename)
{

FILE *fp;
char path[CHAR_BUF_LENGTH];
char cmd[2*CHAR_BUF_LENGTH];

sprintf(cmd,"mcpltool %s",filename);
/* Open the command for reading. */
fp = popen(cmd, "r");
if (fp == NULL) {
printf("Failed to run command %s\n",cmd);
exit(1);
}

/* Read the output a line at a time - output it. */
while (fgets(path, sizeof(path), fp) != NULL) {
printf("%s", path);
}

/* close */
pclose(fp);

return 0;
}
%}

DECLARE
Expand Down Expand Up @@ -201,7 +175,7 @@ INITIALIZE
#endif

if (verbose==1) {
printf("MCPL_output verbose mode: after generating the mcpl-file a summary from mcpltool will be printed.\n");
printf("MCPL_output verbose mode: after generating the mcpl-file a summary will be printed.\n");
}

#if defined (USE_MPI)
Expand Down Expand Up @@ -457,8 +431,8 @@ FINALLY
}
/* Recheck for file existance */
if (mcpl_file_exist(finalfile)) {
printf("\n\nMCPL output summary from 'mcpltool %s'\n",finalfile);
run_mcpltool(finalfile);
printf("\n\nMCPL output summary from %s\n",finalfile);
mcpl_dump(finalfile, 0, 0, 10);
} else {
printf("\n\nWarning, did not localize expected output file for stat summary!\n");
}
Expand Down
55 changes: 49 additions & 6 deletions mcxtrace-comps/misc/MCPL_output.comp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ DECLARE
DArray1d P;
DArray1d U;
int captured;
char finalfile[CHAR_BUF_LENGTH];
%}

INITIALIZE
Expand All @@ -106,7 +107,18 @@ INITIALIZE
sprintf(extension,"mcpl");
#endif
/*add output dir (if applicable) to the output filename and add extension if */
myfilename=mcfull_file(filename,extension);
// Append the extension to the filename
// -- do not use mcfull_file for this since it can not handle absolute filenames with a '.' in them
char * actual_filename = (char *) calloc(strlen(filename)+strlen(extension)+2, sizeof(char));
strcpy(actual_filename, filename);
strcat(actual_filename, ".");
strcat(actual_filename, extension);

// still use mcfull_file in case the filename does not include path information
myfilename = mcfull_file(actual_filename, NULL);

// release the memory now that we have the full filename
if (actual_filename) free(actual_filename);

char line[256];
outputfile = mcpl_create_outfile(myfilename);
Expand All @@ -130,7 +142,7 @@ INITIALIZE
fseek(fp, 0L, SEEK_SET); // seek back to beginning of file
if ( size && (buffer=malloc(size))!=NULL){
if (size!=(fread(buffer,1,size,fp))){
fprintf(stderr,"Warning (%s): Source instrument file not read cleanly\n", NAME_CURRENT_COMP);
fprintf(stderr,"\nWarning (%s): Source instrument file not read cleanly\n", NAME_CURRENT_COMP);
}
mcpl_hdr_add_data(outputfile, "mccode_instr_file", size, buffer);
free(buffer);
Expand Down Expand Up @@ -165,7 +177,7 @@ INITIALIZE
#endif

if (verbose==1) {
printf("MCPL_output verbose mode: after generating the mcpl-file it will be reread and a summary printed.\n");
printf("MCPL_output verbose mode: after generating the mcpl-file a summary will be printed.\n");
}

#if defined (USE_MPI)
Expand All @@ -187,6 +199,9 @@ INITIALIZE
}
}
if (myfilename){
MPI_MASTER(
sprintf(finalfile,myfilename);
);
free(myfilename);
}
#ifndef OPENACC
Expand Down Expand Up @@ -368,13 +383,22 @@ FINALLY
int j;
mcpl_outfile_t merge_outfile;

merge_outfilename=mcfull_file(filename,extension);
char * real_filename = (char *) calloc(strlen(filename) + strlen(extension) + 2, sizeof(char));
strcpy(real_filename, filename);
strcat(real_filename, ".");
strcat(real_filename, extension);

merge_outfilename = mcfull_file(real_filename, NULL);

mpi_node_files=(char **) calloc(mpi_node_count,sizeof(char *));
sprintf(extension,"node_%i.mcpl", mpi_node_count);
char * temp_name = (char *) calloc(strlen(filename) + strlen(extension) + 2, sizeof(char));
for (j=0;j<mpi_node_count;j++){
sprintf(extension,"node_%i.mcpl",j);
mpi_node_files[j]=mcfull_file(filename,extension);
sprintf(temp_name, "%s.node_%i.mcpl", filename, j);
mpi_node_files[j] = mcfull_file(temp_name, NULL);
}
if (temp_name) free(temp_name);

/*now do the merge through the call to mcpl_merge_files*/
merge_outfile = mcpl_merge_files(merge_outfilename,mpi_node_count,(const char **) mpi_node_files);
mcpl_closeandgzip_outfile(merge_outfile);
Expand All @@ -391,6 +415,7 @@ FINALLY
}

/*free the string storage*/
sprintf(finalfile,merge_outfilename);
free(merge_outfilename);
for (j=0;j<mpi_node_count;j++){
free(mpi_node_files[j]);
Expand All @@ -399,6 +424,24 @@ FINALLY
}
);
#endif
if(verbose) {
MPI_MASTER(
/* check if we need to add .gz suffix */
if (!mcpl_file_exist(finalfile)) {
char *finalfilegz=malloc(CHAR_BUF_LENGTH*sizeof(char));
sprintf(finalfilegz,"%s.gz",finalfile);
sprintf(finalfile,"%s",finalfilegz);
free(finalfilegz);
}
/* Recheck for file existance */
if (mcpl_file_exist(finalfile)) {
printf("\n\nMCPL output summary from %s\n",finalfile);
mcpl_dump(finalfile, 0, 0, 10);
} else {
printf("\n\nWarning, did not localize expected output file for stat summary!\n");
}
);
}
%}

MCDISPLAY
Expand Down

0 comments on commit b32e8ea

Please sign in to comment.