example for ExamplePlugin

The functionality of OpenMM will (eventually) include everything that one would need to run modern molecular simulation.
POST REPLY
User avatar
Wei Chen
Posts: 83
Joined: Thu Jul 02, 2015 6:35 pm

example for ExamplePlugin

Post by Wei Chen » Fri Feb 24, 2017 8:30 am

I am trying to run the example plugin (https://github.com/peastman/openmmexampleplugin), but for some reason my example C++ code does not compile (it says "undefined reference to `registerExampleReferenceKernelFactories'"). Could anyone give me a simple example like this: https://github.com/pandegroup/openmm/bl ... ondForce.h, for the example plugin? Thanks!

By the way, following is the example I wrote:

Code: Select all

#include "OpenMM.h"
#include <cstdio>
#include "ExampleForce.h"
#include "openmm/internal/AssertionUtilities.h"
#include "openmm/Context.h"
#include "openmm/Platform.h"
#include "openmm/System.h"
#include "openmm/VerletIntegrator.h"
#include <cmath>
#include <iostream>
#include <vector>

using namespace OpenMM;
using namespace ExamplePlugin;
using namespace std;

extern "C" OPENMM_EXPORT void registerExampleReferenceKernelFactories();

// Forward declaration of routine for printing one frame of the
// trajectory, defined later in this source file.
void writePdbFrame(int frameNum, const OpenMM::State&);

void print_Vec3(Vec3 temp_vec) {
    for (int ii = 0; ii < 3; ii ++) {
        printf("%f\t", temp_vec[ii]);
    }
    printf("\n");
    return;
}

void simulateArgon()
{
    // Load any shared libraries containing GPU implementations.
    OpenMM::Platform::loadPluginsFromDirectory(
        OpenMM::Platform::getDefaultPluginsDirectory());

    // Create a system with nonbonded forces.
    OpenMM::System system;
    // OpenMM::NonbondedForce* nonbond = new OpenMM::NonbondedForce(); 
    // system.addForce(nonbond);
    int num_of_atoms = 6;
    for (int ii = 0; ii < num_of_atoms; ii ++) {
        system.addParticle(1);    
    }
    VerletIntegrator integrator(0.01);
    ExampleForce* forceField = new ExampleForce();
    forceField -> addBond(0, 1, 1, 10); // not sure if the parameter is good
    system.addForce(forceField);
    Platform& platform = Platform::getPlatformByName("Reference");
    Context context(system, integrator, platform);

    vector<Vec3> positions_1(num_of_atoms);
    positions_1[0] = Vec3(-1, -2, -3);
    positions_1[1] = Vec3(0, 1, 0);
    positions_1[2] = Vec3(1, 0, 0);
    positions_1[3] = Vec3(0, 0, 1);
    positions_1[4] = Vec3(0.5, 0, 0);
    positions_1[5] = Vec3(0, 0.3, 0.6);
    context.setPositions(positions_1);


    // Simulate.
    for (int frameNum=1; ;++frameNum) {
        // Output current state information.
        OpenMM::State state    = context.getState(State::Positions | State::Forces);
        const double  timeInPs = state.getTime();
        writePdbFrame(frameNum, state); // output coordinates
        // printf("forces:\n");
        // auto forces = state.getForces();
        // for (int ii = 0; ii < num_of_atoms; ii ++) {
        //     print_Vec3(forces[ii]);
        // }
        if (timeInPs >= 20.)
            break;

        // Advance state many steps at a time, for efficient use of OpenMM.
        integrator.step(10); // (use a lot more than this normally)
    }
    return;
}

int main() 
{
    try {
        registerExampleReferenceKernelFactories();  // this is required
        simulateArgon();
        return 0; // success!
    }
    // Catch and report usage and runtime errors detected by OpenMM and fail.
    catch(const std::exception& e) {
        printf("EXCEPTION: %s\n", e.what());
        return 1; // failure!
    }
}

// Handy homebrew PDB writer for quick-and-dirty trajectory output.
void writePdbFrame(int frameNum, const OpenMM::State& state) 
{
    // Reference atomic positions in the OpenMM State.
    const std::vector<OpenMM::Vec3>& posInNm = state.getPositions();

    // Use PDB MODEL cards to number trajectory frames
    printf("MODEL     %d\n", frameNum); // start of frame
    for (int a = 0; a < (int)posInNm.size(); ++a)
    {
        printf("ATOM  %5d  AR   AR     1    ", a+1); // atom number
        printf("%8.3f%8.3f%8.3f  1.00  0.00\n",      // coordinates
            // "*10" converts nanometers to Angstroms
            posInNm[a][0]*10, posInNm[a][1]*10, posInNm[a][2]*10);
    }
    printf("ENDMDL\n"); // end of frame
    return;
}

and the error is

Code: Select all

g++ -g -std=c++11 -I/home/fisiksnju/.anaconda2/include -I/home/fisiksnju/openmmexampleplugin/openmmapi/include -I/home/fisiksnju/openmmexampleplugin/openmmapi/include/internal -I/home/fisiksnju/.anaconda2/include/openmm/reference/ -I/home/fisiksnju/.anaconda2/include/python2.7/ -I/home/fisiksnju/openmmexampleplugin/platforms/reference/include -I/home/fisiksnju/openmmexampleplugin/platforms/cuda/include -o example temp_example.cpp -L/home/fisiksnju/.anaconda2/lib -lOpenMM -L/home/fisiksnju/.anaconda2/lib -lExamplePlugin
/tmp/ccvIfose.o: In function `main':
/home/fisiksnju/example_for_plugin/temp_example.cpp:85: undefined reference to `registerExampleReferenceKernelFactories'
collect2: error: ld returned 1 exit status

User avatar
Peter Eastman
Posts: 2541
Joined: Thu Aug 09, 2007 1:25 pm

Re: example for ExamplePlugin

Post by Peter Eastman » Fri Feb 24, 2017 10:54 am

You don't normally call that function yourself. It gets called automatically when the plugin is loaded. Instead, tell OpenMM to load all available plugins:

Code: Select all

Platform::loadPluginsFromDirectory(Platform::getDefaultPluginsDirectory());
That should go at the very start of your program.

User avatar
Wei Chen
Posts: 83
Joined: Thu Jul 02, 2015 6:35 pm

Re: example for ExamplePlugin

Post by Wei Chen » Fri Feb 24, 2017 2:58 pm

Thanks Peter,

But if I remove

Code: Select all

registerExampleReferenceKernelFactories(); 
and place:

Code: Select all

OpenMM::Platform::loadPluginsFromDirectory(
        OpenMM::Platform::getDefaultPluginsDirectory());
at the very beginning of my main() function, the code compiles, but I got this runtime error:

Code: Select all

EXCEPTION: Specified a Platform for a Context which does not support all required kernels
.

Do you have any idea why this happens?

Thanks!

User avatar
Peter Eastman
Posts: 2541
Joined: Thu Aug 09, 2007 1:25 pm

Re: example for ExamplePlugin

Post by Peter Eastman » Fri Feb 24, 2017 3:30 pm

Make sure it's actually loading the plugins. Have you installed the example plugin into your OpenMM directory (by doing "make install")? Check that it's finding your plugins directory (print the value of Platform::getDefaultPluginsDirectory()). Also, Platform::loadPluginsFromDirectory() returns the names of all plugins it successfully loaded. And you can call Platform::getPluginLoadFailures() to get information about any that couldn't be loaded.

User avatar
Wei Chen
Posts: 83
Joined: Thu Jul 02, 2015 6:35 pm

Re: example for ExamplePlugin

Post by Wei Chen » Fri Feb 24, 2017 4:38 pm

Now it works, thank you!

User avatar
Wei Chen
Posts: 83
Joined: Thu Jul 02, 2015 6:35 pm

Re: example for ExamplePlugin

Post by Wei Chen » Mon Feb 27, 2017 3:06 pm

Hi Peter,

I still do not get how computation part in CUDA code works in the example plugin. In Reference platform there is an "execute()" function in this file: https://github.com/peastman/openmmexamp ... ernels.cpp that is responsible for calculating force and energy. But in the corresponding file in CUDA: https://github.com/peastman/openmmexamp ... ernels.cpp, "execute()" simply returns 0.0. Could you explain to me how "exampleForce.cu" is integrated with this function? Because I assume that we need to extract the force and current position of every particle from current "context" which is a parameter in "execute()" function, but I do not see how this information is passed into "exampleForce.cu"?

Thanks for your help!

Wei

POST REPLY