/* ****************************************************************************** * * * This program and the accompanying materials are made available under the * terms of the Apache License, Version 2.0 which is available at * https://www.apache.org/licenses/LICENSE-2.0. * * See the NOTICE file distributed with this work for additional * information regarding copyright ownership. * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. * * SPDX-License-Identifier: Apache-2.0 ******************************************************************************/ // // This class implements Workspace functionality in c++ // // // @author raver119@gmail.com // #ifndef LIBND4J_WORKSPACE_H #define LIBND4J_WORKSPACE_H #include #include #include #include #include #include #include namespace sd { namespace memory { class SD_LIB_EXPORT Workspace { protected: char* _ptrHost = nullptr; char* _ptrDevice = nullptr; bool _allocatedHost = false; bool _allocatedDevice = false; std::atomic _offset; std::atomic _offsetSecondary; sd::LongType _initialSize = 0L; sd::LongType _initialSizeSecondary = 0L; sd::LongType _currentSize = 0L; sd::LongType _currentSizeSecondary = 0L; std::mutex _mutexAllocation; std::mutex _mutexSpills; bool _externalized = false; std::vector _spills; std::vector _spillsSecondary; std::atomic _spillsSize; std::atomic _cycleAllocations; std::atomic _spillsSizeSecondary; std::atomic _cycleAllocationsSecondary; void init(sd::LongType primaryBytes, sd::LongType secondaryBytes = 0L); void freeSpills(); public: explicit Workspace(ExternalWorkspace* external); Workspace(sd::LongType initialSize = 0L, sd::LongType secondaryBytes = 0L); ~Workspace(); sd::LongType getAllocatedSize(); sd::LongType getCurrentSize(); sd::LongType getCurrentOffset(); sd::LongType getSpilledSize(); sd::LongType getUsedSize(); sd::LongType getAllocatedSecondarySize(); sd::LongType getCurrentSecondarySize(); sd::LongType getCurrentSecondaryOffset(); sd::LongType getSpilledSecondarySize(); sd::LongType getUsedSecondarySize(); void expandBy(sd::LongType primaryBytes, sd::LongType secondaryBytes = 0L); void expandTo(sd::LongType primaryBytes, sd::LongType secondaryBytes = 0L); // bool resizeSupported(); void* allocateBytes(sd::LongType numBytes); void* allocateBytes(MemoryType type, sd::LongType numBytes); void scopeIn(); void scopeOut(); /* * This method creates NEW workspace of the same memory size and returns pointer to it */ Workspace* clone(); }; } // namespace memory } // namespace sd #endif // LIBND4J_WORKSPACE_H