60f73652a2a7ab1283ae918756e3268bed7570d7
[alexxy/gromacs.git] / src / gromacs / gmxlib / nonbonded / nb_kernel.c
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013,2014, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 #include "gmxpre.h"
36
37 #include <stdio.h>
38 #include <string.h>
39
40 #include "nb_kernel.h"
41 #include "gromacs/utility/smalloc.h"
42 #include "gromacs/utility/cstringutil.h"
43 #include "gromacs/utility/fatalerror.h"
44
45
46 /* Static data structures to find kernels */
47 static nb_kernel_info_t *   kernel_list           = NULL;
48 static unsigned int         kernel_list_size      = 0;
49 static int *                kernel_list_hash      = NULL;
50 static unsigned int         kernel_list_hash_size = 0;
51
52 static unsigned int
53 nb_kernel_hash_func(const char *   arch,
54                     const char *   elec,
55                     const char *   elec_mod,
56                     const char *   vdw,
57                     const char *   vdw_mod,
58                     const char *   geom,
59                     const char *   other,
60                     const char *   vf)
61 {
62     unsigned int hash;
63
64     hash = gmx_string_hash_func(arch, gmx_string_hash_init);
65     hash = gmx_string_hash_func(elec, hash);
66     hash = gmx_string_hash_func(elec_mod, hash);
67     hash = gmx_string_hash_func(vdw, hash);
68     hash = gmx_string_hash_func(vdw_mod, hash);
69     hash = gmx_string_hash_func(geom, hash);
70     hash = gmx_string_hash_func(other, hash);
71     hash = gmx_string_hash_func(vf, hash);
72
73     return hash;
74 }
75
76 void
77 nb_kernel_list_add_kernels(nb_kernel_info_t *   new_kernel_list,
78                            int                  new_kernel_list_size)
79 {
80     srenew(kernel_list, kernel_list_size+new_kernel_list_size);
81     memcpy(kernel_list+kernel_list_size, new_kernel_list, new_kernel_list_size*sizeof(nb_kernel_info_t));
82     kernel_list_size += new_kernel_list_size;
83 }
84
85
86 int
87 nb_kernel_list_hash_init(void)
88 {
89     unsigned int            i;
90     unsigned int            index;
91
92     kernel_list_hash_size   = kernel_list_size*5;
93     snew(kernel_list_hash, kernel_list_hash_size);
94
95     for (i = 0; i < kernel_list_hash_size; i++)
96     {
97         kernel_list_hash[i] = -1;
98     }
99     for (i = 0; i < kernel_list_size; i++)
100     {
101         index = nb_kernel_hash_func(kernel_list[i].architecture,
102                                     kernel_list[i].electrostatics,
103                                     kernel_list[i].electrostatics_modifier,
104                                     kernel_list[i].vdw,
105                                     kernel_list[i].vdw_modifier,
106                                     kernel_list[i].geometry,
107                                     kernel_list[i].other,
108                                     kernel_list[i].vf) % kernel_list_hash_size;
109
110         /* Check for collisions and advance if necessary */
111         while (kernel_list_hash[index] != -1)
112         {
113             index = (index+1) % kernel_list_hash_size;
114         }
115
116         kernel_list_hash[index] = i;
117     }
118     return 0;
119 }
120
121 void
122 nb_kernel_list_hash_destroy()
123 {
124     sfree(kernel_list_hash);
125     kernel_list_hash      = NULL;
126     kernel_list_hash_size = 0;
127 }
128
129
130 nb_kernel_t *
131 nb_kernel_list_findkernel(FILE gmx_unused *   log,
132                           const char *        arch,
133                           const char *        electrostatics,
134                           const char *        electrostatics_modifier,
135                           const char *        vdw,
136                           const char *        vdw_modifier,
137                           const char *        geometry,
138                           const char *        other,
139                           const char *        vf)
140 {
141     int                 i;
142     unsigned int        index;
143     nb_kernel_info_t *  kernelinfo_ptr;
144
145     if (kernel_list_hash_size == 0)
146     {
147         return NULL;
148     }
149
150     index = nb_kernel_hash_func(arch,
151                                 electrostatics,
152                                 electrostatics_modifier,
153                                 vdw,
154                                 vdw_modifier,
155                                 geometry,
156                                 other,
157                                 vf) % kernel_list_hash_size;
158
159     kernelinfo_ptr = NULL;
160     while ( (i = kernel_list_hash[index]) != -1)
161     {
162         if (!gmx_strcasecmp_min(kernel_list[i].architecture, arch) &&
163             !gmx_strcasecmp_min(kernel_list[i].electrostatics, electrostatics) &&
164             !gmx_strcasecmp_min(kernel_list[i].electrostatics_modifier, electrostatics_modifier) &&
165             !gmx_strcasecmp_min(kernel_list[i].vdw, vdw) &&
166             !gmx_strcasecmp_min(kernel_list[i].vdw_modifier, vdw_modifier) &&
167             !gmx_strcasecmp_min(kernel_list[i].geometry, geometry) &&
168             !gmx_strcasecmp_min(kernel_list[i].other, other) &&
169             !gmx_strcasecmp_min(kernel_list[i].vf, vf))
170         {
171             kernelinfo_ptr = kernel_list+i;
172             break;
173         }
174         index = (index+1) % kernel_list_hash_size;
175     }
176
177     if (debug && kernelinfo_ptr != NULL)
178     {
179         fprintf(debug,
180                 "NB kernel %s() with architecture '%s' used for neighborlist with\n"
181                 "    Elec: '%s', Modifier: '%s'\n"
182                 "    Vdw:  '%s', Modifier: '%s'\n"
183                 "    Geom: '%s', Other: '%s', Calc: '%s'\n\n",
184                 kernelinfo_ptr->kernelname, arch, electrostatics, electrostatics_modifier,
185                 vdw, vdw_modifier, geometry, other, vf);
186     }
187
188     /* If we did not find any kernel the pointer will still be NULL */
189     return (kernelinfo_ptr != NULL) ? kernelinfo_ptr->kernelptr : NULL;
190 }