CudaFreeEnergyKernelFactory.cpp 5.9 KB
Newer Older
Mark Friedrichs's avatar
Mark Friedrichs committed
1
2
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
/* -------------------------------------------------------------------------- *
 *                                   OpenMM                                   *
 * -------------------------------------------------------------------------- *
 * 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: Peter Eastman                                                     *
 * 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 "CudaFreeEnergyKernelFactory.h"
#include "CudaFreeEnergyKernels.h"
#include "openmm/freeEnergyKernels.h"
30
#include "FreeEnergyCudaData.h"
Mark Friedrichs's avatar
Mark Friedrichs committed
31
32
#include "openmm/internal/ContextImpl.h"
#include "openmm/OpenMMException.h"
33
#include "kernels/GpuFreeEnergyCudaKernels.h"
Mark Friedrichs's avatar
Mark Friedrichs committed
34
35
36

using namespace OpenMM;

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

40
extern "C" void registerKernelFactories() {
41
42
43
44
45
46
47
48
49
50
51
52
53
54
    // (void) fprintf( stderr, "initOpenMMCudaFreeEnergyPlugin called\n");
    if ( gpuIsAvailableSoftcore() ){
        for( int ii = 0; ii < Platform::getNumPlatforms(); ii++ ){
            Platform& platform = Platform::getPlatform(ii);
            if( platform.getName().compare( "Cuda" ) == 0 ){
                 CudaFreeEnergyKernelFactory* factory = new CudaFreeEnergyKernelFactory();
                 platform.registerKernelFactory(CalcNonbondedSoftcoreForceKernel::Name(), factory);
                 platform.registerKernelFactory(CalcGBSAOBCSoftcoreForceKernel::Name(), factory);
                 platform.registerKernelFactory(CalcGBVISoftcoreForceKernel::Name(), factory);
            }
        }
    }   
}

55
56
57
58
59
60
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
extern "C" OPENMMCUDA_EXPORT void registerFreeEnergyCudaKernelFactories( void ) { 
    int hasCudaPlatform = 0;
    for( int ii = 0; ii < Platform::getNumPlatforms() && hasCudaPlatform == 0; ii++ ){
        Platform& platform = Platform::getPlatform(ii);
        if( platform.getName() == "Cuda" ){
            hasCudaPlatform = 1;
        }   
    }   
    if( hasCudaPlatform == 0 ){
        if (gpuIsAvailable() ){
            Platform::registerPlatform(new CudaPlatform());
        }   
    }   
    registerKernelFactories();
}

static std::map<ContextImpl*, FreeEnergyCudaData*> contextToFreeEnergyDataMap;

// look up FreeEnergyCudaData for input contextImpl in contextToFreeEnergyDataMap

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

// remove FreeEnergyCudaData from contextToFreeEnergyDataMap

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

Mark Friedrichs's avatar
Mark Friedrichs committed
92
93
KernelImpl* CudaFreeEnergyKernelFactory::createKernelImpl(std::string name, const Platform& platform, ContextImpl& context) const {

94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
    // create FreeEnergyCudaData object if contextToFreeEnergyDataMap does not contain
    // key equal to current context

    FreeEnergyCudaData* freeEnergyCudaData;
    std::map<ContextImpl*, FreeEnergyCudaData*>::const_iterator mapIterator  = contextToFreeEnergyDataMap.find(&context);

    if( mapIterator == contextToFreeEnergyDataMap.end() ){
        CudaPlatform::PlatformData& cudaPlatformData = *static_cast<CudaPlatform::PlatformData*>(context.getPlatformData());
        freeEnergyCudaData                           = new FreeEnergyCudaData( cudaPlatformData );
        contextToFreeEnergyDataMap[&context]         = freeEnergyCudaData;
        //freeEnergyCudaData->setLog( stderr );
        freeEnergyCudaData->setContextImpl( static_cast<void*>(&context) );
    } else {
        freeEnergyCudaData                           = mapIterator->second;
    }
Mark Friedrichs's avatar
Mark Friedrichs committed
109
110

    if (name == CalcNonbondedSoftcoreForceKernel::Name())
111
        return new CudaFreeEnergyCalcNonbondedSoftcoreForceKernel(name, platform, *freeEnergyCudaData, context.getSystem());
Mark Friedrichs's avatar
Mark Friedrichs committed
112
113

    if (name == CalcGBSAOBCSoftcoreForceKernel::Name())
114
        return new CudaFreeEnergyCalcGBSAOBCSoftcoreForceKernel(name, platform, *freeEnergyCudaData);
Mark Friedrichs's avatar
Mark Friedrichs committed
115
116

    if (name == CalcGBVISoftcoreForceKernel::Name())
117
        return new CudaFreeEnergyCalcGBVISoftcoreForceKernel(name, platform, *freeEnergyCudaData);
Mark Friedrichs's avatar
Mark Friedrichs committed
118
119
120

    throw OpenMMException( (std::string("Tried to create kernel with illegal kernel name '") + name + "'").c_str() );
}