Fenix @develop
 
Loading...
Searching...
No Matches
buffer.hpp
1/*
2//@HEADER
3// ************************************************************************
4//
5//
6// _|_|_|_| _|_|_|_| _| _| _|_|_| _| _|
7// _| _| _|_| _| _| _| _|
8// _|_|_| _|_|_| _| _| _| _| _|
9// _| _| _| _|_| _| _| _|
10// _| _|_|_|_| _| _| _|_|_| _| _|
11//
12//
13//
14//
15// Copyright (C) 2016 Rutgers University and Sandia Corporation
16//
17// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
18// the U.S. Government retains certain rights in this software.
19//
20// Redistribution and use in source and binary forms, with or without
21// modification, are permitted provided that the following conditions are
22// met:
23//
24// 1. Redistributions of source code must retain the above copyright
25// notice, this list of conditions and the following disclaimer.
26//
27// 2. Redistributions in binary form must reproduce the above copyright
28// notice, this list of conditions and the following disclaimer in the
29// documentation and/or other materials provided with the distribution.
30//
31// 3. Neither the name of the Corporation nor the names of the
32// contributors may be used to endorse or promote products derived from
33// this software without specific prior written permission.
34//
35// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
36// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
39// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
40// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
41// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
42// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
43// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
44// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
45// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46//
47// Author Marc Gamell, Eric Valenzuela, Keita Teranishi, Manish Parashar,
48// Michael Heroux, and Matthew Whitlock
49//
50// Questions? Contact Keita Teranishi (knteran@sandia.gov) and
51// Marc Gamell (mgamell@cac.rutgers.edu)
52//
53// ************************************************************************
54//@HEADER
55*/
56
57#ifndef FENIX_DATA_BUFFER_HPP
58#define FENIX_DATA_BUFFER_HPP
59
60#include <memory>
61#include <vector>
62
63#include <mpi.h>
64#include "fenix/mpixx/tasks.hpp"
65
66namespace fenix::data::util {
67
69 public:
70 using MPITask = mpixx::MPITask;
71
72 DataBuffer() = default;
73 explicit DataBuffer(size_t init_size) { resize(init_size); }
74
75 ~DataBuffer() { free_buf(); }
76
77 DataBuffer(const DataBuffer& o) = delete;
78
79 DataBuffer(DataBuffer&& o) { *this = std::move(o); }
80 DataBuffer& operator=(DataBuffer&& o) {
81 if (&o == this) return *this;
82 free_buf();
83 *this = o;
84 o.release_buf();
85 return *this;
86 }
87
88 void take_ownership(char* new_buf, size_t new_size) {
89 free_buf();
90 buf = new_buf;
91 user_size = new_size;
92 alloc_size = new_size;
93 }
94
95 void take_ownership_mmapped(char* new_buf, size_t new_size) {
96 free_buf();
97 buf = new_buf;
98 user_size = new_size;
99 alloc_size = new_size;
100 mmap_buffer = true;
101 }
102
103 // Simple resize without overallocating.
104 // No ammortized growth cost, but we don't need it for our usage
105 void resize(size_t new_size);
106 void shrink_to_fit();
107 void clear() { resize(0); }
108
109 // Set to new size, discarding old data if reallocation is needed
110 void reset(size_t new_size = 0) {
111 resize(0);
112 resize(new_size);
113 }
114
115 void reserve(size_t new_size) {
116 size_t old_size = user_size;
117 resize(new_size);
118 resize(old_size);
119 }
120
121 char* data() { return buf; }
122 const char* data() const { return buf; }
123 size_t size() const { return user_size; }
124
125 MPITask send(int dst, int tag, MPI_Comm comm);
126
127 // Recv n bytes
128 MPITask recv(int n, int src, int tag, MPI_Comm comm);
129
130 // Recv an unknown amount of data and resize to fit
131 MPITask recv_unknown(int src, int tag, MPI_Comm comm);
132
133 private:
134 char* buf = nullptr;
135 size_t user_size = 0;
136 size_t alloc_size = 0;
137 bool mmap_buffer = false;
138
139 DataBuffer& operator=(const DataBuffer& o) {
140 this->buf = o.buf;
141 this->user_size = o.user_size;
142 this->alloc_size = o.alloc_size;
143 this->mmap_buffer = o.mmap_buffer;
144 return *this;
145 }
146 void free_buf();
147 void release_buf() {
148 buf = nullptr;
149 user_size = 0;
150 alloc_size = 0;
151 mmap_buffer = false;
152 }
153 void realloc_buf(size_t new_size);
154};
155
156} // namespace fenix::data::util
157
158#endif //FENIX_DATA_BUFFER_HPP
Definition buffer.hpp:68
Definition task.hpp:15