$ NUM_THREADS=1 ./rtoa-pgatram-allinone2b t1.txt t1.txt t1.txt t1.txt | cksum do_it_all time : 0.498 secs 737201628 75552000 $ NUM_THREADS=4 ./rtoa-pgatram-allinone2b t1.txt t1.txt t1.txt t1.txt | cksum do_it_all time : 0.176 secs 737201628 75552000 $ NUM_THREADS=8 ./rtoa-pgatram-allinone2b t1.txt t1.txt t1.txt t1.txt | cksum do_it_all time : 0.124 secs 737201628 75552000 $ NUM_THREADS=16 ./rtoa-pgatram-allinone2b t1.txt t1.txt t1.txt t1.txt | cksum do_it_all time : 0.096 secs 737201628 75552000 #### $ NUM_THREADS=1 ./rtoa-pgatram-allinone2c t1.txt t1.txt t1.txt t1.txt | cksum do_it_all time : 0.488 secs 737201628 75552000 $ NUM_THREADS=4 ./rtoa-pgatram-allinone2c t1.txt t1.txt t1.txt t1.txt | cksum do_it_all time : 0.143 secs 737201628 75552000 $ NUM_THREADS=8 ./rtoa-pgatram-allinone2c t1.txt t1.txt t1.txt t1.txt | cksum do_it_all time : 0.091 secs 737201628 75552000 $ NUM_THREADS=16 ./rtoa-pgatram-allinone2c t1.txt t1.txt t1.txt t1.txt | cksum do_it_all time : 0.065 secs 737201628 75552000 #### // rtoa-pgatram-allinone2c.cpp. Crude allinone version. // based on rtoa-pgatram-allinone2.cpp https://perlmonks.org/?node_id=11152186 // // Obtain the fast_io library (required dependency): // git clone --depth=1 https://github.com/cppfastio/fast_io // // Obtain the Portable Memory Mapping C++ Class (required dependency): // git clone --depth=1 https://github.com/stbrumme/portable-memory-mapping // Copy two files to your source directory: *.cpp and *.h // cp portable-memory-mapping/MemoryMapped.* . // // Compile with g++ or clang++: // clang++ -o rtoa-pgatram-allinone2c -std=c++20 -fopenmp -Wall -O3 -I fast_io/include MemoryMapped.cpp rtoa-pgatram-allinone2c.cpp // // OpenMP Little Book: // https://nanxiao.gitbooks.io/openmp-little-book/content/ #include #include #include #include #include #include #ifdef _OPENMP #include #endif #include #include // See [id://11149504] for more info on the fast_io C++ library #include #include #include "MemoryMapped.h" // --------------------------------------------------------------- typedef std::chrono::high_resolution_clock high_resolution_clock; typedef std::chrono::high_resolution_clock::time_point time_point; typedef std::chrono::milliseconds milliseconds; double elaspe_time( time_point cend, time_point cstart) { return double ( std::chrono::duration_cast(cend - cstart).count() ) * 1e-3; } // --------------------------------------------------------------- // Though there are less than 256 initializers in this ascii table, // the others are guaranteed by ANSI C to be initialized to zero. static const int romtab[256] = { 0,0,0,0,0,0, 0, 0, 0, 0, // 00- 09 0,0,0,0,0,0, 0, 0, 0, 0, // 10- 19 0,0,0,0,0,0, 0, 0, 0, 0, // 20- 29 0,0,0,0,0,0, 0, 0, 0, 0, // 30- 39 0,0,0,0,0,0, 0, 0, 0, 0, // 40- 49 0,0,0,0,0,0, 0, 0, 0, 0, // 50- 59 0,0,0,0,0,0, 0, 100, 500, 0, // 60- 69 0,0,0,1,0,0, 50,1000, 0, 0, // 70- 79 0,0,0,0,0,0, 5, 0, 10, 0, // 80- 89 0,0,0,0,0,0, 0, 0, 0, 100, // 90- 99 500,0,0,0,0,1, 0, 0, 50,1000, // 100-109 0,0,0,0,0,0, 0, 0, 5, 0, // 110-119 10,0,0,0,0,0, 0, 0, 0, 0 // 120-129 }; // Return the arabic number for a roman letter c. // Return zero if the roman letter c is invalid. inline int urtoa(int c) { return romtab[c]; } inline int accfn(int t, char c) { return t + urtoa(c) - t % urtoa(c) * 2; } inline int roman_to_dec(std::string_view st) { return std::accumulate(st.begin(), st.end(), 0, accfn); } // --------------------------------------------------------------- inline constexpr auto MIN_CHUNK_SIZE = 1048576; // Helper funtion to get the next up value for integer division. static std::size_t divide_up(std::size_t dividend, std::size_t divisor) { if (dividend % divisor) return dividend / divisor + 1; else return dividend / divisor; } // Read an input file of Roman Numerals and do it all. static void do_it_all( std::string_view fname, // in: file name containing a list of Roman Numerals int nthds // in: number of threads ) { // Map file to memory. MemoryMapped loader(fname.data(), MemoryMapped::WholeFile, MemoryMapped::SequentialScan); if (!loader.isValid()) { fast_io::io::perrln("Error opening '", fname, "' : ", std::errc{errno}); return; } // Get raw pointer to mapped memory. char* data = (char*) loader.getData(); std::size_t filesize = loader.size(); // Loop contiguous file container. Each thread process max 1 chunk. std::size_t chunk_size = divide_up(filesize, nthds * 1); if (chunk_size < MIN_CHUNK_SIZE) chunk_size = MIN_CHUNK_SIZE; std::size_t num_chunks = divide_up(filesize, chunk_size); char const *end{data + filesize - 1}; fast_io::out_buf_type obf{fast_io::out()}; #pragma omp parallel for ordered schedule(static, 1) for (std::size_t chunk_id = 0; chunk_id < num_chunks; ++chunk_id) { char const *last{data + filesize - 1}; char const *first{data}; std::string buf; if (chunk_id < num_chunks - 1) { last = first + (chunk_size * (chunk_id + 1) - 1); if (*last != '\n') last = fast_io::find_lf(last, end); } if (chunk_id > 0) { first += (chunk_size * chunk_id - 1); if (*first != '\n') first = fast_io::find_lf(first, end); ++first; } while (first <= last) { auto start_ptr{first}; first = fast_io::find_lf(first, last); auto end_ptr{first}; // chunk ids greater than 0 store locally for later output int dec = roman_to_dec(std::string_view(start_ptr, end_ptr - start_ptr)); if (chunk_id) buf.append(fast_io::concatln(dec)); else fast_io::io::println(obf, dec); ++first; } #pragma omp ordered { if (chunk_id) fast_io::io::print(obf, buf); } } } int main(int argc, char* argv[]) { if (argc < 2) { if (argc > 0) std::cerr << "usage: rtoa-pgatram-allinone2c file... >out.txt\n"; return 1; } // Get the list of input files from the command line int nfiles = argc - 1; char** fname = &argv[1]; std::cerr << std::setprecision(3) << std::setiosflags(std::ios::fixed); time_point cstartall, cendall; cstartall = high_resolution_clock::now(); #ifdef _OPENMP // Determine the number of threads. int nthds = std::thread::hardware_concurrency(); const char* env_nthds1 = std::getenv("OMP_NUM_THREADS"); const char* env_nthds2 = std::getenv("NUM_THREADS"); if (env_nthds1 && strlen(env_nthds1)) nthds = ::atoi(env_nthds1); else if (env_nthds2 && strlen(env_nthds2)) nthds = ::atoi(env_nthds2); omp_set_dynamic(false); omp_set_num_threads(nthds); #else int nthds = 1; #endif for (int i = 0; i < nfiles; ++i) do_it_all(fname[i], nthds); cendall = high_resolution_clock::now(); double ctakenall = elaspe_time(cendall, cstartall); std::cerr << "do_it_all time : " << std::setw(8) << ctakenall << " secs\n"; return 0; }