@@ -192,6 +192,29 @@ inline shared_context_ptr get_var_context(const std::string &file) {
192192 return std::make_shared<stan::io::dump>(var_context);
193193}
194194
195+ std::vector<std::string> make_filenames (const std::string &filename,
196+ const std::string &tag,
197+ const std::string &type,
198+ unsigned int num_chains,
199+ unsigned int id) {
200+ std::vector<std::string> names (num_chains);
201+ auto base_sfx = get_basename_suffix (filename);
202+ if (base_sfx.second .empty ()) {
203+ base_sfx.second = type;
204+ }
205+ auto name_iterator = [num_chains, id](auto i) {
206+ if (num_chains == 1 ) {
207+ return std::string (" " );
208+ } else {
209+ return std::string (" _" + std::to_string (i + id));
210+ }
211+ };
212+ for (int i = 0 ; i < num_chains; ++i) {
213+ names[i] = base_sfx.first + tag + name_iterator (i) + base_sfx.second ;
214+ }
215+ return names;
216+ }
217+
195218using context_vector = std::vector<shared_context_ptr>;
196219/* *
197220 * Make a vector of shared pointers to contexts.
@@ -201,7 +224,8 @@ using context_vector = std::vector<shared_context_ptr>;
201224 * @param num_chains The number of chains to run
202225 * @return a std vector of shared pointers to var contexts
203226 */
204- context_vector get_vec_var_context (const std::string &file, size_t num_chains) {
227+ context_vector get_vec_var_context (const std::string &file, size_t num_chains,
228+ unsigned int id) {
205229 using stan::io::var_context;
206230 if (num_chains == 1 ) {
207231 return context_vector (1 , get_var_context (file));
@@ -249,8 +273,9 @@ context_vector get_vec_var_context(const std::string &file, size_t num_chains) {
249273 " \t Consider saving your data in JSON format instead."
250274 << std::endl;
251275 }
252- std::string file_1
253- = std::string (file_name + " _" + std::to_string (1 ) + file_ending);
276+
277+ auto filenames = make_filenames (file_name, " " , file_ending, num_chains, id);
278+ auto &file_1 = filenames[0 ];
254279 std::fstream stream_1 (file_1.c_str (), std::fstream::in);
255280 // if file_1 exists we'll assume num_chains of these files exist
256281 if (stream_1.rdstate () & std::ifstream::failbit) {
@@ -274,9 +299,8 @@ context_vector get_vec_var_context(const std::string &file, size_t num_chains) {
274299 ret.reserve (num_chains);
275300 ret.push_back (make_context (file_1, stream_1, file_ending));
276301 for (size_t i = 1 ; i < num_chains; ++i) {
277- std::string file_i
278- = std::string (file_name + " _" + std::to_string (i) + file_ending);
279- std::fstream stream_i (file_1.c_str (), std::fstream::in);
302+ auto &file_i = filenames[i];
303+ std::fstream stream_i (file_i.c_str (), std::fstream::in);
280304 // If any stream fails here something went wrong with file names
281305 if (stream_i.rdstate () & std::ifstream::failbit) {
282306 std::string file_name_err = std::string (
@@ -737,29 +761,6 @@ void check_file_config(argument_parser &parser) {
737761 }
738762}
739763
740- std::vector<std::string> make_filenames (const std::string &filename,
741- const std::string &tag,
742- const std::string &type,
743- unsigned int num_chains,
744- unsigned int id) {
745- std::vector<std::string> names (num_chains);
746- auto base_sfx = get_basename_suffix (filename);
747- if (base_sfx.second .empty ()) {
748- base_sfx.second = type;
749- }
750- auto name_iterator = [num_chains, id](auto i) {
751- if (num_chains == 1 ) {
752- return std::string (" " );
753- } else {
754- return std::string (" _" + std::to_string (i + id));
755- }
756- };
757- for (int i = 0 ; i < num_chains; ++i) {
758- names[i] = base_sfx.first + tag + name_iterator (i) + base_sfx.second ;
759- }
760- return names;
761- }
762-
763764void init_callbacks (
764765 argument_parser &parser,
765766 std::vector<stan::callbacks::unique_stream_writer<std::ofstream>>
0 commit comments