Enable -march=native in OS X clang builds
[hoomd-blue.git] / libhoomd / potentials / PotentialBondGPU.h
bloba70d9e834826be7f83f78c3a6dafad3af3cd490a
1 /*
2 Highly Optimized Object-oriented Many-particle Dynamics -- Blue Edition
3 (HOOMD-blue) Open Source Software License Copyright 2009-2014 The Regents of
4 the University of Michigan All rights reserved.
6 HOOMD-blue may contain modifications ("Contributions") provided, and to which
7 copyright is held, by various Contributors who have granted The Regents of the
8 University of Michigan the right to modify and/or distribute such Contributions.
10 You may redistribute, use, and create derivate works of HOOMD-blue, in source
11 and binary forms, provided you abide by the following conditions:
13 * Redistributions of source code must retain the above copyright notice, this
14 list of conditions, and the following disclaimer both in the code and
15 prominently in any materials provided with the distribution.
17 * Redistributions in binary form must reproduce the above copyright notice, this
18 list of conditions, and the following disclaimer in the documentation and/or
19 other materials provided with the distribution.
21 * All publications and presentations based on HOOMD-blue, including any reports
22 or published results obtained, in whole or in part, with HOOMD-blue, will
23 acknowledge its use according to the terms posted at the time of submission on:
24 http://codeblue.umich.edu/hoomd-blue/citations.html
26 * Any electronic documents citing HOOMD-Blue will link to the HOOMD-Blue website:
27 http://codeblue.umich.edu/hoomd-blue/
29 * Apart from the above required attributions, neither the name of the copyright
30 holder nor the names of HOOMD-blue's contributors may be used to endorse or
31 promote products derived from this software without specific prior written
32 permission.
34 Disclaimer
36 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS'' AND
37 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
38 WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND/OR ANY
39 WARRANTIES THAT THIS SOFTWARE IS FREE OF INFRINGEMENT ARE DISCLAIMED.
41 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
42 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
43 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
44 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
45 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
46 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
47 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50 // Maintainer: joaander
52 #ifndef __POTENTIAL_BOND_GPU_H__
53 #define __POTENTIAL_BOND_GPU_H__
55 #ifdef ENABLE_CUDA
57 #include <boost/bind.hpp>
59 #include "PotentialBond.h"
60 #include "PotentialBondGPU.cuh"
61 #include "Autotuner.h"
63 /*! \file PotentialBondGPU.h
64 \brief Defines the template class for standard bond potentials on the GPU
65 \note This header cannot be compiled by nvcc
68 #ifdef NVCC
69 #error This header cannot be compiled by nvcc
70 #endif
72 //! Template class for computing bond potentials on the GPU
74 /*!
75 \tparam evaluator EvaluatorBond class used to evaluate V(r) and F(r)/r
76 \tparam gpu_cgbf Driver function that calls gpu_compute_bond_forces<evaluator>()
78 \sa export_PotentialBondGPU()
80 template< class evaluator, cudaError_t gpu_cgbf(const bond_args_t& bond_args,
81 const typename evaluator::param_type *d_params,
82 unsigned int *d_flags) >
83 class PotentialBondGPU : public PotentialBond<evaluator>
85 public:
86 //! Construct the bond potential
87 PotentialBondGPU(boost::shared_ptr<SystemDefinition> sysdef,
88 const std::string& log_suffix="");
89 //! Destructor
90 virtual ~PotentialBondGPU() {}
92 //! Set autotuner parameters
93 /*! \param enable Enable/disable autotuning
94 \param period period (approximate) in time steps when returning occurs
96 virtual void setAutotunerParams(bool enable, unsigned int period)
98 PotentialBond<evaluator>::setAutotunerParams(enable, period);
99 m_tuner->setPeriod(period);
100 m_tuner->setEnabled(enable);
103 protected:
104 boost::scoped_ptr<Autotuner> m_tuner; //!< Autotuner for block size
105 GPUArray<unsigned int> m_flags; //!< Flags set during the kernel execution
107 //! Actually compute the forces
108 virtual void computeForces(unsigned int timestep);
111 template< class evaluator, cudaError_t gpu_cgbf(const bond_args_t& bond_args,
112 const typename evaluator::param_type *d_params,
113 unsigned int *d_flags) >
114 PotentialBondGPU< evaluator, gpu_cgbf >::PotentialBondGPU(boost::shared_ptr<SystemDefinition> sysdef,
115 const std::string& log_suffix)
116 : PotentialBond<evaluator>(sysdef, log_suffix)
118 // can't run on the GPU if there aren't any GPUs in the execution configuration
119 if (!this->exec_conf->isCUDAEnabled())
121 this->m_exec_conf->msg->error() << "Creating a PotentialBondGPU with no GPU in the execution configuration" << std::endl;
122 throw std::runtime_error("Error initializing PotentialBondGPU");
125 // allocate and zero device memory
126 GPUArray<typename evaluator::param_type> params(this->m_bond_data->getNTypes(), this->exec_conf);
127 this->m_params.swap(params);
129 // allocate flags storage on the GPU
130 GPUArray<unsigned int> flags(1, this->exec_conf);
131 m_flags.swap(flags);
133 // reset flags
134 ArrayHandle<unsigned int> h_flags(m_flags,access_location::host, access_mode::overwrite);
135 h_flags.data[0] = 0;
137 m_tuner.reset(new Autotuner(32, 1024, 32, 5, 100000, "harmonic_bond", this->m_exec_conf));
140 template< class evaluator, cudaError_t gpu_cgbf(const bond_args_t& bond_args,
141 const typename evaluator::param_type *d_params,
142 unsigned int *d_flags) >
143 void PotentialBondGPU< evaluator, gpu_cgbf >::computeForces(unsigned int timestep)
145 // start the profile
146 if (this->m_prof) this->m_prof->push(this->exec_conf, this->m_prof_name);
148 // access the particle data
149 ArrayHandle<Scalar4> d_pos(this->m_pdata->getPositions(), access_location::device, access_mode::read);
150 ArrayHandle<Scalar> d_diameter(this->m_pdata->getDiameters(), access_location::device, access_mode::read);
151 ArrayHandle<Scalar> d_charge(this->m_pdata->getCharges(), access_location::device, access_mode::read);
152 BoxDim box = this->m_pdata->getBox();
154 // access parameters
155 ArrayHandle<typename evaluator::param_type> d_params(this->m_params, access_location::device, access_mode::read);
157 // access net force & virial
158 ArrayHandle<Scalar4> d_force(this->m_force, access_location::device, access_mode::readwrite);
159 ArrayHandle<Scalar> d_virial(this->m_virial, access_location::device, access_mode::readwrite);
162 const GPUArray<typename BondData::members_t>& gpu_bond_list = this->m_bond_data->getGPUTable();
163 const Index2D& gpu_table_indexer = this->m_bond_data->getGPUTableIndexer();
165 ArrayHandle<typename BondData::members_t> d_gpu_bondlist(gpu_bond_list, access_location::device, access_mode::read);
166 ArrayHandle<unsigned int > d_gpu_n_bonds(this->m_bond_data->getNGroupsArray(),
167 access_location::device, access_mode::read);
169 // access the flags array for overwriting
170 ArrayHandle<unsigned int> d_flags(m_flags, access_location::device, access_mode::readwrite);
172 this->m_tuner->begin();
173 gpu_cgbf(bond_args_t(d_force.data,
174 d_virial.data,
175 this->m_virial.getPitch(),
176 this->m_pdata->getN(),
177 this->m_pdata->getMaxN(),
178 d_pos.data,
179 d_charge.data,
180 d_diameter.data,
181 box,
182 d_gpu_bondlist.data,
183 gpu_table_indexer,
184 d_gpu_n_bonds.data,
185 this->m_bond_data->getNTypes(),
186 this->m_tuner->getParam(),
187 this->m_exec_conf->getComputeCapability()),
188 d_params.data,
189 d_flags.data);
192 if (this->exec_conf->isCUDAErrorCheckingEnabled())
194 CHECK_CUDA_ERROR();
196 // check the flags for any errors
197 ArrayHandle<unsigned int> h_flags(m_flags, access_location::host, access_mode::read);
199 if (h_flags.data[0] & 1)
201 this->m_exec_conf->msg->error() << "bond." << evaluator::getName() << ": bond out of bounds (" << h_flags.data[0] << ")" << endl << endl;
202 throw std::runtime_error("Error in bond calculation");
205 this->m_tuner->end();
207 if (this->m_prof) this->m_prof->pop(this->exec_conf);
210 //! Export this bond potential to python
211 /*! \param name Name of the class in the exported python module
212 \tparam T Class type to export. \b Must be an instantiated PotentialPairGPU class template.
213 \tparam Base Base class of \a T. \b Must be PotentialPair<evaluator> with the same evaluator as used in \a T.
215 template < class T, class Base > void export_PotentialBondGPU(const std::string& name)
217 boost::python::class_<T, boost::shared_ptr<T>, boost::python::bases<Base>, boost::noncopyable >
218 (name.c_str(), boost::python::init< boost::shared_ptr<SystemDefinition>, const std::string& >())
222 #endif // ENABLE_CUDA
223 #endif // __POTENTIAL_PAIR_GPU_H__