33 using reference = std::add_lvalue_reference_t<element_type>;
38 : ptr_(reinterpret_cast<
pointer>(alloc_coll(fpath, mlock))) {
53 : ptr_(ufp.ptr_) { ufp.ptr_ =
nullptr; }
63 : ptr_(ufp.ptr_) { ufp.ptr_ =
nullptr; }
74 explicit operator bool() const noexcept {
82 std::size_t
size() const noexcept {
84 return fm.size() /
sizeof(T);
119 static void* alloc_coll(
const std::string& fpath,
bool mlock) {
124 constexpr std::size_t max_chars = 256;
125 if (fpath.size() >= max_chars) {
126 common::die(
"File path length for unique_file_ptr must be less than %ld.", max_chars);
128 std::array<char, max_chars> buf;
129 std::strncpy(buf.data(), fpath.c_str(), max_chars - 1);
135 common::die(
"Collective operations for ityr::global_vector must be executed on the root thread or SPMD region.");
139 static void free_coll(
void* addr) {
145 common::die(
"Collective operations for ityr::global_vector must be executed on the root thread or SPMD region.");
152 template <
typename T1,
typename T2>
154 return ufp1.
get() == ufp2.
get();
157 template <
typename T>
159 return ufp.
get() ==
nullptr;
162 template <
typename T>
164 return nullptr == ufp.
get();
167 template <
typename T1,
typename T2>
169 return ufp1.
get() != ufp2.
get();
172 template <
typename T>
174 return ufp.
get() !=
nullptr;
177 template <
typename T>
179 return nullptr != ufp.
get();
182 template <
typename T1,
typename T2>
184 return ufp1.
get() > ufp2.
get();
187 template <
typename T>
189 return ufp.
get() >
nullptr;
192 template <
typename T>
194 return nullptr > ufp.
get();
197 template <
typename T1,
typename T2>
199 return ufp1.
get() >= ufp2.
get();
202 template <
typename T>
204 return ufp.
get() >=
nullptr;
207 template <
typename T>
209 return nullptr >= ufp.
get();
212 template <
typename T1,
typename T2>
214 return ufp1.
get() < ufp2.
get();
217 template <
typename T>
219 return ufp.
get() <
nullptr;
222 template <
typename T>
224 return nullptr < ufp.
get();
227 template <
typename T1,
typename T2>
229 return ufp1.
get() <= ufp2.
get();
232 template <
typename T>
234 return ufp.
get() <=
nullptr;
237 template <
typename T>
239 return nullptr <= ufp.
get();
242 template <
typename T>
247 template <
typename CharT,
typename Traits,
typename T>
248 inline std::basic_ostream<CharT, Traits>&
249 operator<<(std::basic_ostream<CharT, Traits>& ostream,
const unique_file_ptr<T>& ufp) {
250 ostream << ufp.get();
259 template <
typename T>
264 ITYR_TEST_CASE(
"[ityr::unique_file_ptr] unique_file_ptr") {
271 std::string filename =
"test.bin";
274 std::vector<long> buf(n);
275 for (
long i = 0; i < n; i++) {
278 std::ofstream ostream(filename, std::ios::binary);
279 ostream.write(
reinterpret_cast<char*
>(buf.data()), buf.size() *
sizeof(
long));
285 unique_file_ptr<long> fp = make_unique_file<long>(filename);
288 for (
long i = 0; i < n; i++) {
299 unique_file_ptr<long> fp = make_unique_file<long>(filename);
303 execution::parallel_policy(100),
304 count_iterator<long>(0),
305 count_iterator<long>(n),
307 [](
long i,
long v) { ITYR_CHECK(v == i); });
314 remove(filename.c_str());
Definition: unique_file_ptr.hpp:29
void swap(unique_file_ptr< T > ufp)
Definition: unique_file_ptr.hpp:108
unique_file_ptr(const unique_file_ptr &)=delete
T element_type
Definition: unique_file_ptr.hpp:31
pointer operator->() const noexcept
Definition: unique_file_ptr.hpp:98
std::size_t size() const noexcept
Definition: unique_file_ptr.hpp:82
element_type * pointer
Definition: unique_file_ptr.hpp:32
unique_file_ptr & operator=(unique_file_ptr &&ufp) noexcept
Definition: unique_file_ptr.hpp:54
reference operator*() const
Definition: unique_file_ptr.hpp:87
unique_file_ptr(unique_file_ptr &&ufp) noexcept
Definition: unique_file_ptr.hpp:52
unique_file_ptr(const std::string &fpath, bool mlock=false)
Definition: unique_file_ptr.hpp:37
unique_file_ptr(unique_file_ptr< U > &&ufp) noexcept
Definition: unique_file_ptr.hpp:62
std::add_lvalue_reference_t< element_type > reference
Definition: unique_file_ptr.hpp:33
pointer release() noexcept
Definition: unique_file_ptr.hpp:102
constexpr unique_file_ptr() noexcept
Definition: unique_file_ptr.hpp:35
unique_file_ptr & operator=(std::nullptr_t) noexcept
Definition: unique_file_ptr.hpp:46
pointer get() const noexcept
Definition: unique_file_ptr.hpp:78
~unique_file_ptr()
Definition: unique_file_ptr.hpp:72
unique_file_ptr & operator=(unique_file_ptr< U > &&ufp) noexcept
Definition: unique_file_ptr.hpp:65
unique_file_ptr & operator=(const unique_file_ptr &)=delete
reference operator[](std::size_t i) const
Definition: unique_file_ptr.hpp:92
unique_file_ptr(std::nullptr_t) noexcept
Definition: unique_file_ptr.hpp:45
#define ITYR_SUBCASE(name)
Definition: util.hpp:41
#define ITYR_CHECK(cond)
Definition: util.hpp:48
MPI_Comm mpicomm()
Definition: topology.hpp:206
rank_t my_rank()
Definition: topology.hpp:207
void mpi_barrier(MPI_Comm comm)
Definition: mpi_util.hpp:42
void fini()
Definition: ito.hpp:45
auto root_exec(Fn &&fn, Args &&... args)
Definition: ito.hpp:50
auto coll_exec(const Fn &fn, const Args &... args)
Definition: ito.hpp:72
void init(MPI_Comm comm=MPI_COMM_WORLD)
Definition: ito.hpp:41
bool is_root()
Definition: ito.hpp:66
bool is_spmd()
Definition: ito.hpp:61
void fini()
Definition: ori.hpp:49
void init(MPI_Comm comm=MPI_COMM_WORLD)
Definition: ori.hpp:45
file_mem & file_mem_get(void *addr)
Definition: ori.hpp:252
void file_mem_free_coll(void *addr)
Definition: ori.hpp:248
void * file_mem_alloc_coll(const std::string &fpath, bool mlock)
Definition: ori.hpp:244
Definition: allocator.hpp:16
rank_t my_rank()
Return the rank of the process running the current thread.
Definition: ityr.hpp:99
bool operator==(const global_vector< T > &x, const global_vector< T > &y)
Definition: global_vector.hpp:669
bool operator<=(const unique_file_ptr< T1 > &ufp1, const unique_file_ptr< T2 > &ufp2)
Definition: unique_file_ptr.hpp:228
bool operator!=(const global_vector< T > &x, const global_vector< T > &y)
Definition: global_vector.hpp:676
unique_file_ptr< T > make_unique_file(const std::string &fpath, bool mlock=false)
Definition: unique_file_ptr.hpp:260
void for_each(const ExecutionPolicy &policy, ForwardIterator first, ForwardIterator last, Op op)
Apply an operator to each element in a range.
Definition: parallel_loop.hpp:136
std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &ostream, const unique_file_ptr< T > &ufp)
Definition: unique_file_ptr.hpp:249
void swap(global_vector< T > &v1, global_vector< T > &v2) noexcept
Definition: global_vector.hpp:664
bool operator>=(const unique_file_ptr< T1 > &ufp1, const unique_file_ptr< T2 > &ufp2)
Definition: unique_file_ptr.hpp:198
bool operator<(const unique_file_ptr< T1 > &ufp1, const unique_file_ptr< T2 > &ufp2)
Definition: unique_file_ptr.hpp:213
bool operator>(const unique_file_ptr< T1 > &ufp1, const unique_file_ptr< T2 > &ufp2)
Definition: unique_file_ptr.hpp:183
void swap(unique_file_ptr< T > ufp1, unique_file_ptr< T > ufp2)
Definition: unique_file_ptr.hpp:243