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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 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 92 93 94 95 96 97 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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
/* This program is free software; you can redistribute it and/or modify
* it under the terms of version 3 or later of the GNU General Public License as
* published by the Free Software Foundation.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*
* cache.c
*
* by Gary Wong, 1997-2000
* $Id: cache.c,v 1.27 2009/08/30 20:56:32 Superfly_Jon Exp $
*/
#include "config.h"
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#ifndef WIN32
#include <stdio.h>
#endif
#include "cache.h"
#if USE_MULTITHREAD
#include "multithread.h"
#define cache_lock(pc, k) \
if (MT_SafeIncCheck(&(pc->entries[k].lock))) \
WaitForLock(&(pc->entries[k].lock))
#define cache_unlock(pc, k) MT_SafeDec(&(pc->entries[k].lock))
static void WaitForLock(int *lock)
{
do
{
MT_SafeDec(lock);
} while (MT_SafeIncCheck(lock));
}
#endif
/* Adapted from
http://burtleburtle.net/bob/c/lookup2.c
--------------------------------------------------------------------
lookup2.c, by Bob Jenkins, December 1996, Public Domain.
hash(), hash2(), hash3, and hashmix() are externally useful functions.
Routines to test the hash are included if SELF_TEST is defined.
You can use this free for any purpose. It has no warranty.
--------------------------------------------------------------------
*/
typedef unsigned long ub4;
int CacheCreate(evalCache* pc, unsigned int s)
{
#if CACHE_STATS
pc->cLookup = 0;
pc->cHit = 0;
pc->nAdds = 0;
#endif
pc->size = s;
/* adjust size to smallest power of 2 GE to s */
while ((s & (s-1)) != 0)
s &= (s-1);
pc->size = (s < pc->size) ? 2 * s : s;
pc->hashMask = (pc->size >> 1) - 1;
pc->entries = (cacheNode*)malloc((pc->size / 2) * sizeof(*pc->entries));
if (pc->entries == 0)
return -1;
CacheFlush(pc);
return 0;
}
extern unsigned long GetHashKey(unsigned long hashMask, const cacheNodeDetail* e)
{
ub4 a = 0x9e3779b9; /* the golden ratio; an arbitrary value */
ub4 b = a;
ub4 c = 11 + (unsigned int)e->nEvalContext;
c = c + (((short)e->key.data[2]) << 8);
b = b + e->key.data[1];
a = a + e->key.data[0];
/* hashmix macro expanded here */
a = (a - b - c) ^ (c >> 13);
b = (b - c - a) ^ (a << 8);
c = (c - a - b) ^ (b >> 13);
a = (a - b - c) ^ (c >> 12);
b = (b - c - a) ^ (a << 16);
c = (c - a - b) ^ (b >> 5);
a = (a - b - c) ^ (c >> 3);
b = (b - c - a) ^ (a << 10);
c = (c - a - b) ^ (b >> 15);
return (c & hashMask);
}
unsigned int CacheLookupWithLocking(evalCache* pc, const cacheNodeDetail* e, float *arOut, float *arCubeful)
{
unsigned long l = GetHashKey(pc->hashMask, e);
#if CACHE_STATS
++pc->cLookup;
#endif
#if USE_MULTITHREAD
cache_lock(pc, l);
#endif
if ((pc->entries[l].nd_primary.nEvalContext != e->nEvalContext ||
memcmp(pc->entries[l].nd_primary.key.auch, e->key.auch, sizeof(e->key.auch)) != 0))
{ /* Not in primary slot */
if ((pc->entries[l].nd_secondary.nEvalContext != e->nEvalContext ||
memcmp(pc->entries[l].nd_secondary.key.auch, e->key.auch, sizeof(e->key.auch)) != 0))
{ /* Cache miss */
#if USE_MULTITHREAD
cache_unlock(pc, l);
#endif
return l;
}
else
{ /* Found in second slot, promote "hot" entry */
cacheNodeDetail tmp = pc->entries[l].nd_primary;
pc->entries[l].nd_primary = pc->entries[l].nd_secondary;
pc->entries[l].nd_secondary = tmp;
}
}
/* Cache hit */
#if CACHE_STATS
++pc->cHit;
#endif
memcpy(arOut, pc->entries[l].nd_primary.ar, sizeof(float) * 5/*NUM_OUTPUTS*/ );
if (arCubeful)
*arCubeful = pc->entries[l].nd_primary.ar[5]; /* Cubeful equity stored in slot 5 */
#if USE_MULTITHREAD
cache_unlock(pc, l);
#endif
return CACHEHIT;
}
unsigned int CacheLookupNoLocking(evalCache* pc, const cacheNodeDetail* e, float *arOut, float *arCubeful)
{
unsigned long l = GetHashKey(pc->hashMask, e);
#if CACHE_STATS
++pc->cLookup;
#endif
if ((pc->entries[l].nd_primary.nEvalContext != e->nEvalContext ||
memcmp(pc->entries[l].nd_primary.key.auch, e->key.auch, sizeof(e->key.auch)) != 0))
{ /* Not in primary slot */
if ((pc->entries[l].nd_secondary.nEvalContext != e->nEvalContext ||
memcmp(pc->entries[l].nd_secondary.key.auch, e->key.auch, sizeof(e->key.auch)) != 0))
{ /* Cache miss */
return l;
}
else
{ /* Found in second slot, promote "hot" entry */
cacheNodeDetail tmp = pc->entries[l].nd_primary;
pc->entries[l].nd_primary = pc->entries[l].nd_secondary;
pc->entries[l].nd_secondary = tmp;
}
}
/* Cache hit */
#if CACHE_STATS
++pc->cHit;
#endif
memcpy(arOut, pc->entries[l].nd_primary.ar, sizeof(float) * 5/*NUM_OUTPUTS*/ );
if (arCubeful)
*arCubeful = pc->entries[l].nd_primary.ar[5]; /* Cubeful equity stored in slot 5 */
return CACHEHIT;
}
void CacheAddWithLocking(evalCache* pc, const cacheNodeDetail* e, unsigned long l)
{
#if USE_MULTITHREAD
cache_lock(pc, l);
#endif
pc->entries[l].nd_secondary = pc->entries[l].nd_primary;
pc->entries[l].nd_primary = *e;
#if USE_MULTITHREAD
cache_unlock(pc, l);
#endif
#if CACHE_STATS
++pc->nAdds;
#endif
}
void CacheDestroy(const evalCache* pc)
{
free(pc->entries);
}
void CacheFlush(const evalCache* pc)
{
unsigned int k;
for(k = 0; k < pc->size / 2; ++k) {
pc->entries[k].nd_primary.nEvalContext = -1;
pc->entries[k].nd_secondary.nEvalContext = -1;
#if USE_MULTITHREAD
pc->entries[k].lock = 0;
#endif
}
}
int CacheResize(evalCache *pc, unsigned int cNew)
{
if (cNew != pc->size)
{
CacheDestroy(pc);
if (CacheCreate(pc, cNew) != 0)
return -1;
}
return (int)pc->size;
}
void CacheStats(const evalCache* pc, unsigned int* pcLookup, unsigned int* pcHit, unsigned int* pcUsed)
{
#if CACHE_STATS
if ( pcLookup )
*pcLookup = pc->cLookup;
if ( pcHit )
*pcHit = pc->cHit;
if( pcUsed )
*pcUsed = pc->nAdds;
#else
if ( pcLookup )
*pcLookup = 0;
if ( pcHit )
*pcHit = 0;
if ( pcUsed )
*pcUsed = 0;
#endif
}
|