<commit_before>/**
 * GPU-resident Dataset
 *
 * Author: <NAME> (<EMAIL>)
 * Created on: 2014-11-13
 * Last Modified: 2014-11-13
 */

#ifndef GRD_H
#define GRD_H

#include "logging.h"

/**
 * GPU-Resident Dataset (GRD) is allocated on host pinned memory, which is
 * accessible by all CUDA contexts.
 */
template<typename T>
class GRD {
 public:
    T *     elemsHost;    // Points to the host-allocated buffer
    T *     elemsDevice;  // Points to the GPU-allocated buffer
    size_t  length;       // The length of the buffer

    /**
     * Allocate the host- and device-resident buffer of length `len`.
     */
    explicit GRD(size_t len) {
        length = len;
        elemsHost = reinterrupt_cast<T *> malloc(elemsHost, len * sizeof(T));
        CUDA_CHECK(cudaMallocHost(reinterpret_cast<void **>(&elemsDevice),
                                  len * sizeof(T), cudaHostAllocPortable));
    }

    /**
     * Overloads the subscript to access an element on host side.
     * Do not check the boundaries for speed.
     */
    __host__
    inline T& operator[] (int index) const {
        return elemsHost[index];
    }

    /**
     * Overloads the subscript to access an element on device side.
     */
    __device__
    inline T& operator[] (int index) const {
        return elemsDevice[index];
    }

    /** Write-back the dataset from GPU's onboard memory to host memory */
    void persist(void) {
        CUDA_CHECK(cudaMemcpy(elemsDevice, elemsHost,
                              size * sizeof(T), cudaMemcpyDefault));
    }

    /** Cache the dataset in GPU's onboard memory */
    void cache(void) {
        CUDA_CHECK(cudaMemcpy(elemsDevice, elemsHost,
                              size * sizeof(T), cudaMemcpyDefault));
    }

    /** Free both host- and device- resident buffers */
    ~GRD(void) {
        free(elemsHost);
        CUDA_CHECK(cudaFreeHost(elemsHost));
    }
};

#endif  // GRD_H<commit_msg>[Feature] Add a grd.h to manage memory buffer<commit_after>/**
 * GPU-resident Dataset
 *
 * Author: <NAME> (<EMAIL>)
 * Created on: 2014-11-13
 * Last Modified: 2014-11-13
 */

#ifndef GRD_H
#define GRD_H

#include "logging.h"

/**
 * GPU-Resident Dataset (GRD) provides the utility for allocating data buffers
 * which can be transferred between CPU and GPU and accessed from both CPU and GPU.
 * GRD is allocated on host pinned memory, which is accessible by all CUDA contexts.
 */
template<typename T>
class GRD {
 private:
    T *     elemsHost;    /** Points to the host-allocated buffer */
    T *     elemsDevice;  /** Points to the GPU-allocated buffer */
    size_t  length;       /** The length of the buffer */

 public:
    /** List Initializer */
    GRD() : elemsHost(NULL), elemsDevice(NULL), length(0) {}

    /**
     * Overloads the subscript to access an element on host side.
     * Do not check the boundaries for speed.
     */
    __host__ __device__
    inline T& operator[] (size_t index) const {
#ifdef __CUDA_ARCH__
        return elemsDevice[index];
#else
        return elemsHost[index];
#endif
    }

    /** Returning the size */
    inline size_t size(void) const {
        return length;
    }

    /**
     * Allocate the host- and device-resident buffer of length `len`.
     */
    inline void reserve(size_t len) {
        length = len;
        elemsHost = reinterpret_cast<T *>(malloc(len * sizeof(T)));
        CUDA_CHECK(cudaMallocHost(reinterpret_cast<void **>(&elemsDevice),
                                  len * sizeof(T), cudaHostAllocPortable));
    }

    /** 
     * Write-backs the dataset from GPU's on-board memory to host memory.
     */
    inline void persist(void) {
        CUDA_CHECK(cudaMemcpy(elemsDevice, elemsHost,
                              size * sizeof(T), cudaMemcpyDefault));
    }

    /** 
     * Caches the dataset in GPU's on-board memory.
     */
    inline void cache(void) {
        CUDA_CHECK(cudaMemcpy(elemsDevice, elemsHost,
                              size * sizeof(T), cudaMemcpyDefault));
    }

    /** 
     * Free both host- and device- resident buffers.
     */
    inline void del(void) {
        if (elemsHost)   free(elemsHost);
        if (elemsDevice) CUDA_CHECK(cudaFreeHost(elemsDevice));
    }

    ~GRD(void) {
        del();
    }
};

#endif  // GRD_H
{'fraction_non_alphanumeric': 0.11059262715818946, 'fraction_numerical': 0.007699486700886608, 'mean_word_length': 2.2452687358062073, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5783098', 'n_tokens_mistral': 1380, 'n_tokens_neox': 1308, 'n_words': 387}