Skip to content

Commit

Permalink
reduce memory usage on score function, longlong => char
Browse files Browse the repository at this point in the history
  • Loading branch information
krypdkat committed Apr 12, 2024
1 parent 2f98f05 commit 62a1153
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
20 changes: 12 additions & 8 deletions src/score.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ template<
struct ScoreFunction
{
long long miningData[dataLength];
//neuron only has values [-1, 0, 1]
struct
{
long long input[dataLength + numberOfInputNeurons + infoLength];
long long output[infoLength + numberOfOutputNeurons + dataLength];
long long buffer[dataLength + numberOfInputNeurons + infoLength];
char input[dataLength + numberOfInputNeurons + infoLength];
char output[infoLength + numberOfOutputNeurons + dataLength];
char buffer[dataLength + numberOfInputNeurons + infoLength];
} _neurons[solutionBufferCount];
struct
{
Expand All @@ -52,7 +53,7 @@ struct ScoreFunction
int _bufferPos[solutionBufferCount][numberOfOutputNeurons + dataLength][129];
#endif
int nSample;
long long _sumBuffer[solutionBufferCount][dataLength + numberOfInputNeurons + infoLength];
char _sumBuffer[solutionBufferCount][dataLength + numberOfInputNeurons + infoLength];
unsigned short _indices[solutionBufferCount][dataLength + numberOfInputNeurons + infoLength];

m256i initialRandomSeed;
Expand Down Expand Up @@ -261,7 +262,7 @@ struct ScoreFunction
}

void getLastNeurons(const unsigned short* indices, const int* bucket, const int* modNum, unsigned short* topMax,
const int nMax, const int totalModNum, int& currentCount, long long* neuron, int* index)
const int nMax, const int totalModNum, int& currentCount, char* neuron, int* index)
{
while (currentCount < nMax)
{
Expand All @@ -285,7 +286,7 @@ struct ScoreFunction
}

short getLastNeuronsIndex(const unsigned short* indices, const int* bucket, const int* modNum,
const int totalModNum, long long* neuron, int* index)
const int totalModNum, char* neuron, int* index)
{
int current_max = -1;
int max_id = -1;
Expand Down Expand Up @@ -359,7 +360,7 @@ struct ScoreFunction
template <bool isInput, int beginLength, int neuronLength, int endLength, int duration>
void computeNeuron(int solutionBufIdx)
{
long long* neurons = nullptr;
char* neurons = nullptr;
char* synapses = nullptr;
if (isInput) {
neurons = _neurons[solutionBufIdx].input;
Expand Down Expand Up @@ -462,7 +463,10 @@ struct ScoreFunction

// compute input
setMem(&neurons.input[0], sizeof(neurons.input), 0);
copyMem(&neurons.input[0], miningData, sizeof(miningData));
for (int i = 0; i < dataLength; i++)
{
neurons.input[i] = (char)(miningData[i]);
}
computeBucket<true, dataLength, numberOfInputNeurons, infoLength>(solutionBufIdx);
computeNeuron<true, dataLength, numberOfInputNeurons, infoLength, maxInputDuration>(solutionBufIdx);

Expand Down
8 changes: 4 additions & 4 deletions test/score.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,16 @@ struct ScoreTester
std::cout << "current score() returns " << current << ", reference score() returns " << reference << std::endl;
bool matchedInputNeuron = true;
bool matchedOutputNeuron = true;
for (int i = 0; i < 1024 + 2048 + 512; i++) {
for (int i = 0; i < dataLength + numberOfInputNeurons + infoLength; i++) {
if ((*score)._neurons[0].input[i] != (*score_ref_impl)._neurons[0].input[i]) {
printf("DIFF Input neuron %d: %lld vs %lld\n", i, (*score)._neurons[0].input[i], (*score_ref_impl)._neurons[0].input[i]);
printf("DIFF Input neuron %d: %d vs %d\n", i, (*score)._neurons[0].input[i], (*score_ref_impl)._neurons[0].input[i]);
matchedInputNeuron = false;
break;
}
}
for (int i = 0; i < 1024 + 2048 + 512; i++) {
for (int i = 0; i < dataLength + numberOfOutputNeurons + infoLength; i++) {
if ((*score)._neurons[0].output[i] != (*score_ref_impl)._neurons[0].output[i]) {
printf("DIFF Output neuron %d: %lld vs %lld\n", i, (*score)._neurons[0].output[i], (*score_ref_impl)._neurons[0].output[i]);
printf("DIFF Output neuron %d: %d vs %d\n", i, (*score)._neurons[0].output[i], (*score_ref_impl)._neurons[0].output[i]);
matchedOutputNeuron = false;
break;
}
Expand Down
15 changes: 10 additions & 5 deletions test/score_reference.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ template<
struct ScoreReferenceImplementation
{
long long miningData[dataLength];

//neuron only has values [-1, 0, 1]
struct
{
long long input[dataLength + numberOfInputNeurons + infoLength];
long long output[infoLength + numberOfOutputNeurons + dataLength];
long long neuronBuffer[dataLength + numberOfInputNeurons + infoLength];
char input[dataLength + numberOfInputNeurons + infoLength];
char output[infoLength + numberOfOutputNeurons + dataLength];
char neuronBuffer[dataLength + numberOfInputNeurons + infoLength];
} _neurons[solutionBufferCount];
struct
{
Expand All @@ -41,7 +43,7 @@ struct ScoreReferenceImplementation
}
}

static inline void clampNeuron(long long& val)
static inline void clampNeuron(char& val)
{
if (val > NEURON_VALUE_LIMIT) {
val = NEURON_VALUE_LIMIT;
Expand Down Expand Up @@ -90,7 +92,10 @@ struct ScoreReferenceImplementation
synapses.outputLength[outputNeuronIndex * (infoLength + numberOfOutputNeurons + dataLength) + (infoLength + outputNeuronIndex)] = 0;
}

memcpy(&neurons.input[0], &miningData, sizeof(miningData));
for (int i = 0; i < dataLength; i++)
{
neurons.input[i] = (char)(miningData[i]);
}

for (int tick = 1; tick <= maxInputDuration; tick++)
{
Expand Down

0 comments on commit 62a1153

Please sign in to comment.