<commit_before>#ifndef NNGRIDDER_H
#define NNGRIDDER_H

#include <iostream>
#include <string>
#include <algorithm>
#include <fstream>
#include <boost/program_options.hpp>
#include <boost/multi_array.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/math/quaternion.hpp>
#include <cuda.h>
#include <thrust/system/cuda/experimental/pinned_allocator.h>
#include <vector_types.h>
#include "atom_type.h"
#include "box.h"
#include "gridoptions.h"
#include "molgetter.h"
#include "gridmaker.h"

using namespace std;

/* Maintains atom grid information.  Stores a model of the receptor/ligand with
 * MolGetter, but also numerical grids for every protein/ligand atom type.
 */
class NNGridder {
    //modeled from thurst, but with destroy method to make boost happy
    class float_pinned_allocator {
      public:
        typedef float value_type;
        typedef float* pointer;
        typedef const float* const_pointer;
        typedef float& reference;
        typedef const float& const_reference;
        typedef std::size_t size_type;
        typedef std::ptrdiff_t difference_type;

        pointer address(reference r) {
          return &r;
        }
        const_pointer address(const_reference r) {
          return &r;
        }

        //allocate to pinned memory
        pointer allocate(size_type cnt, const_pointer = 0) {
          if (cnt > this->max_size()) {
            throw std::bad_alloc();
          }

          pointer result(0);
          cudaError_t error = cudaMallocHost(reinterpret_cast<void**>(&result),
              cnt * sizeof(value_type));

          if (error) {
            throw std::bad_alloc();
          }

          return result;
        }

        void deallocate(pointer p, size_type cnt) {
          cudaFreeHost(p);
<commit_msg>Fix gninagrid so it uses the host allocator when no cuda-capable device is available.<commit_after>#ifndef NNGRIDDER_H
#define NNGRIDDER_H

#include <iostream>
#include <string>
#include <algorithm>
#include <fstream>
#include <boost/program_options.hpp>
#include <boost/multi_array.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/math/quaternion.hpp>
#include <cuda.h>
#include <thrust/system/cuda/experimental/pinned_allocator.h>
#include <vector_types.h>
#include "atom_type.h"
#include "box.h"
#include "gridoptions.h"
#include "molgetter.h"
#include "gridmaker.h"

using namespace std;

/* Maintains atom grid information.  Stores a model of the receptor/ligand with
 * MolGetter, but also numerical grids for every protein/ligand atom type.
 */
class NNGridder {
    //modeled from thurst, but with destroy method to make boost happy
    class float_pinned_allocator {
      public:
        typedef float value_type;
        typedef float* pointer;
        typedef const float* const_pointer;
        typedef float& reference;
        typedef const float& const_reference;
        typedef std::size_t size_type;
        typedef std::ptrdiff_t difference_type;
        bool std_alloc;

        float_pinned_allocator() : std_alloc(true) {
          int count = 0;
          cudaGetDeviceCount(&count);
          if (count) 
            std_alloc = false;
        }

        pointer address(reference r) {
          return &r;
        }
        const_pointer address(const_reference r) {
          return &r;
        }

        //allocate to pinned memory
        pointer allocate(size_type cnt, const_pointer = 0) {
          if (cnt > this->max_size()) {
            throw std::bad_alloc();
          }

          pointer result(0);
          if (std_alloc) {
            std::allocator<value_type> host_allocator;
            result = host_allocator.allocate(cnt);
          }
          else {
            cudaError_t error = cudaMallocHost(reinterpret_cast<void**>(&result),
                cnt * sizeof(value_type));
            if (error) {
              cout << "CUDA error " << cudaGetErrorName(error) << "\n";
              throw std::bad_alloc();
            }
          }

          return result;
        }

        void deallocate(pointer p, size_type cnt) {
          if (std_alloc) {
            std::allocator<value_type> host_allocator;
            host_allocator.deallocate(p, cnt);
          }
          else {
            cudaFreeHost(p);
          }
{'fraction_non_alphanumeric': 0.09870848708487084, 'fraction_numerical': 0.0011531365313653136, 'mean_word_length': 2.458532695374801, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5736649', 'n_tokens_mistral': 1317, 'n_tokens_neox': 1285, 'n_words': 332}