AmoebaCudaKernelFactory.cpp 8 KB
Newer Older
Mark Friedrichs's avatar
Mark Friedrichs committed
1
/* -------------------------------------------------------------------------- *
2
 *                              OpenMMAmoeba                                  *
Mark Friedrichs's avatar
Mark Friedrichs committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 * -------------------------------------------------------------------------- *
 * This is part of the OpenMM molecular simulation toolkit originating from   *
 * Simbios, the NIH National Center for Physics-Based Simulation of           *
 * Biological Structures at Stanford, funded under the NIH Roadmap for        *
 * Medical Research, grant U54 GM072970. See https://simtk.org.               *
 *                                                                            *
 * Portions copyright (c) 2008 Stanford University and the Authors.           *
 * Authors:                                                                   *
 * Contributors:                                                              *
 *                                                                            *
 * This program is free software: you can redistribute it and/or modify       *
 * it under the terms of the GNU Lesser General Public License as published   *
 * by the Free Software Foundation, either version 3 of the License, or       *
 * (at your option) any later version.                                        *
 *                                                                            *
 * This program is distributed in the hope that it will be useful,            *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
 * GNU Lesser General Public License for more details.                        *
 *                                                                            *
 * You should have received a copy of the GNU Lesser General Public License   *
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.      *
 * -------------------------------------------------------------------------- */

#include "AmoebaCudaKernelFactory.h"
#include "AmoebaCudaKernels.h"
#include "CudaPlatform.h"
#include "AmoebaCudaData.h"
#include "openmm/internal/ContextImpl.h"
#include "openmm/OpenMMException.h"

using namespace OpenMM;

36
37
38
extern "C" void registerPlatforms() {
}

39
extern "C" OPENMMCUDA_EXPORT void registerKernelFactories() {
Mark Friedrichs's avatar
Mark Friedrichs committed
40
//fprintf( stderr,"In registerKernelFactories AmoebaCudaKernelFactory\n" ); fflush( stderr );
Mark Friedrichs's avatar
Mark Friedrichs committed
41
42
    for( int ii = 0; ii < Platform::getNumPlatforms(); ii++ ){
        Platform& platform = Platform::getPlatform(ii);
Mark Friedrichs's avatar
Mark Friedrichs committed
43
        if( platform.getName() == "Cuda" ){
44

Mark Friedrichs's avatar
Mark Friedrichs committed
45
             AmoebaCudaKernelFactory* factory = new AmoebaCudaKernelFactory();
46

Mark Friedrichs's avatar
Mark Friedrichs committed
47
             platform.registerKernelFactory(CalcAmoebaHarmonicBondForceKernel::Name(), factory);
Mark Friedrichs's avatar
Mark Friedrichs committed
48
             platform.registerKernelFactory(CalcAmoebaUreyBradleyForceKernel::Name(), factory);
Mark Friedrichs's avatar
Mark Friedrichs committed
49
50
51
52
53
54
55
56
57
58
59
             platform.registerKernelFactory(CalcAmoebaHarmonicAngleForceKernel::Name(), factory);
             platform.registerKernelFactory(CalcAmoebaHarmonicInPlaneAngleForceKernel::Name(), factory);
             platform.registerKernelFactory(CalcAmoebaTorsionForceKernel::Name(), factory);
             platform.registerKernelFactory(CalcAmoebaPiTorsionForceKernel::Name(), factory);
             platform.registerKernelFactory(CalcAmoebaStretchBendForceKernel::Name(), factory);
             platform.registerKernelFactory(CalcAmoebaOutOfPlaneBendForceKernel::Name(), factory);
             platform.registerKernelFactory(CalcAmoebaTorsionTorsionForceKernel::Name(), factory);
             platform.registerKernelFactory(CalcAmoebaMultipoleForceKernel::Name(), factory);
             platform.registerKernelFactory(CalcAmoebaGeneralizedKirkwoodForceKernel::Name(), factory);
             platform.registerKernelFactory(CalcAmoebaVdwForceKernel::Name(), factory);
             platform.registerKernelFactory(CalcAmoebaWcaDispersionForceKernel::Name(), factory);
60
             platform.registerKernelFactory(CalcAmoebaForcesAndEnergyKernel::Name(), factory);
Mark Friedrichs's avatar
Mark Friedrichs committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
        }
    }
}

static std::map<ContextImpl*, AmoebaCudaData*> contextToAmoebaDataMap;

// look up AmoebaCudaData for input contextImpl in contextToAmoebaDataMap

extern "C" void* getAmoebaCudaData( ContextImpl& context ) {
    std::map<ContextImpl*, AmoebaCudaData*>::const_iterator mapIterator  = contextToAmoebaDataMap.find(&context);
    if( mapIterator == contextToAmoebaDataMap.end() ){
        return NULL;
    } else {
        return static_cast<void*>(mapIterator->second);
    }
}

// remove AmoebaCudaData from contextToAmoebaDataMap

extern "C" void removeAmoebaCudaDataFromContextMap( void* inputContext ) {
    ContextImpl* context = static_cast<ContextImpl*>(inputContext);
    contextToAmoebaDataMap.erase( context );
    return;
}

KernelImpl* AmoebaCudaKernelFactory::createKernelImpl(std::string name, const Platform& platform, ContextImpl& context) const {
    CudaPlatform::PlatformData& cudaPlatformData = *static_cast<CudaPlatform::PlatformData*>(context.getPlatformData());

    // create AmoebaCudaData object if contextToAmoebaDataMap does not contain
    // key equal to current context

    AmoebaCudaData* amoebaCudaData;
    std::map<ContextImpl*, AmoebaCudaData*>::const_iterator mapIterator  = contextToAmoebaDataMap.find(&context);
    if( mapIterator == contextToAmoebaDataMap.end() ){
        amoebaCudaData                         = new AmoebaCudaData( cudaPlatformData );
        contextToAmoebaDataMap[&context]       = amoebaCudaData;
Mark Friedrichs's avatar
Mark Friedrichs committed
97
        //amoebaCudaData->setLog( stderr );
Mark Friedrichs's avatar
Mark Friedrichs committed
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
        amoebaCudaData->setContextImpl( static_cast<void*>(&context) );
    } else {
        amoebaCudaData                         = mapIterator->second;
    }

    if (name == CalcAmoebaHarmonicBondForceKernel::Name())
        return new CudaCalcAmoebaHarmonicBondForceKernel(name, platform, *amoebaCudaData, context.getSystem());

    if (name == CalcAmoebaHarmonicAngleForceKernel::Name())
        return new CudaCalcAmoebaHarmonicAngleForceKernel(name, platform, *amoebaCudaData, context.getSystem());

    if (name == CalcAmoebaHarmonicInPlaneAngleForceKernel::Name())
        return new CudaCalcAmoebaHarmonicInPlaneAngleForceKernel(name, platform, *amoebaCudaData, context.getSystem());

    if (name == CalcAmoebaTorsionForceKernel::Name())
        return new CudaCalcAmoebaTorsionForceKernel(name, platform, *amoebaCudaData, context.getSystem());

    if (name == CalcAmoebaPiTorsionForceKernel::Name())
        return new CudaCalcAmoebaPiTorsionForceKernel(name, platform, *amoebaCudaData, context.getSystem());

    if (name == CalcAmoebaStretchBendForceKernel::Name())
        return new CudaCalcAmoebaStretchBendForceKernel(name, platform, *amoebaCudaData, context.getSystem());

    if (name == CalcAmoebaOutOfPlaneBendForceKernel::Name())
        return new CudaCalcAmoebaOutOfPlaneBendForceKernel(name, platform, *amoebaCudaData, context.getSystem());

    if (name == CalcAmoebaTorsionTorsionForceKernel::Name())
        return new CudaCalcAmoebaTorsionTorsionForceKernel(name, platform, *amoebaCudaData, context.getSystem());

    if (name == CalcAmoebaMultipoleForceKernel::Name())
        return new CudaCalcAmoebaMultipoleForceKernel(name, platform, *amoebaCudaData, context.getSystem());

    if (name == CalcAmoebaGeneralizedKirkwoodForceKernel::Name())
        return new CudaCalcAmoebaGeneralizedKirkwoodForceKernel(name, platform, *amoebaCudaData, context.getSystem());

    if (name == CalcAmoebaVdwForceKernel::Name())
        return new CudaCalcAmoebaVdwForceKernel(name, platform, *amoebaCudaData, context.getSystem());

    if (name == CalcAmoebaWcaDispersionForceKernel::Name())
        return new CudaCalcAmoebaWcaDispersionForceKernel(name, platform, *amoebaCudaData, context.getSystem());

Mark Friedrichs's avatar
Mark Friedrichs committed
139
140
141
    if (name == CalcAmoebaUreyBradleyForceKernel::Name())
        return new CudaCalcAmoebaUreyBradleyForceKernel(name, platform, *amoebaCudaData, context.getSystem());

142
143
144
    if (name == CalcAmoebaForcesAndEnergyKernel::Name())
        return new CalcAmoebaForcesAndEnergyKernel(name, platform, *amoebaCudaData);

Mark Friedrichs's avatar
Mark Friedrichs committed
145
146
    throw OpenMMException((std::string("Tried to create kernel with illegal kernel name '")+name+"'").c_str());
}