Skip to content

Commit 270a4f9

Browse files
committed
Dataset size increased to 2080 MiB
Implemented dataset base offset Tweaked SuperscalarHash constants to prevent register collisions
1 parent f66da39 commit 270a4f9

19 files changed

+56
-44
lines changed

src/asm/program_read_dataset_sshash_init.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@
1313
mov ebx, ebp ;# ecx = ma
1414
and ebx, 2147483584 ;# align "ma" to the start of a cache line
1515
shr ebx, 6 ;# ebx = Dataset block number
16+
;# add ebx, datasetOffset / 64
1617
;# call 32768

src/asm/program_sshash_constants.inc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ r0_mul:
22
;#/ 6364136223846793005
33
db 45, 127, 149, 76, 45, 244, 81, 88
44
r1_add:
5-
;#/ 9298410992540426748
6-
db 252, 161, 245, 89, 136, 151, 10, 129
5+
;#/ 9298411001130361340
6+
db 252, 161, 245, 89, 138, 151, 10, 129
77
r2_add:
88
;#/ 12065312585734608966
99
db 70, 216, 194, 56, 223, 153, 112, 167
1010
r3_add:
11-
;#/ 9306329213124610396
12-
db 92, 9, 34, 191, 28, 185, 38, 129
11+
;#/ 9306329213124626780
12+
db 92, 73, 34, 191, 28, 185, 38, 129
1313
r4_add:
1414
;#/ 5281919268842080866
1515
db 98, 138, 159, 23, 151, 37, 77, 73

src/common.hpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ along with RandomX. If not, see<http://www.gnu.org/licenses/>.
2828
namespace randomx {
2929

3030
static_assert((RANDOMX_ARGON_MEMORY & (RANDOMX_ARGON_MEMORY - 1)) == 0, "RANDOMX_ARGON_MEMORY must be a power of 2.");
31-
static_assert((RANDOMX_DATASET_SIZE & (RANDOMX_DATASET_SIZE - 1)) == 0, "RANDOMX_DATASET_SIZE must be a power of 2.");
32-
static_assert(RANDOMX_DATASET_SIZE <= 4294967296ULL, "RANDOMX_DATASET_SIZE must not exceed 4294967296.");
31+
static_assert((RANDOMX_DATASET_BASE_SIZE & (RANDOMX_DATASET_BASE_SIZE - 1)) == 0, "RANDOMX_DATASET_BASE_SIZE must be a power of 2.");
32+
static_assert(RANDOMX_DATASET_BASE_SIZE <= 4294967296ULL, "RANDOMX_DATASET_BASE_SIZE must not exceed 4294967296.");
33+
static_assert(RANDOMX_DATASET_EXTRA_SIZE % 64 == 0, "RANDOMX_DATASET_EXTRA_SIZE must be divisible by 64.");
3334
static_assert(RANDOMX_PROGRAM_SIZE > 0, "RANDOMX_PROGRAM_SIZE must be greater than 0");
3435
static_assert(RANDOMX_PROGRAM_ITERATIONS > 0, "RANDOMX_PROGRAM_ITERATIONS must be greater than 0");
3536
static_assert(RANDOMX_PROGRAM_COUNT > 0, "RANDOMX_PROGRAM_COUNT must be greater than 0");
@@ -56,8 +57,10 @@ namespace randomx {
5657
constexpr int ArgonSaltSize = sizeof(RANDOMX_ARGON_SALT) - 1;
5758
constexpr int CacheLineSize = RANDOMX_DATASET_ITEM_SIZE;
5859
constexpr int ScratchpadSize = RANDOMX_SCRATCHPAD_L3;
59-
constexpr uint32_t CacheLineAlignMask = (RANDOMX_DATASET_SIZE - 1) & ~(CacheLineSize - 1);
60-
constexpr uint32_t CacheSize = RANDOMX_ARGON_MEMORY * 1024;
60+
constexpr uint32_t CacheLineAlignMask = (RANDOMX_DATASET_BASE_SIZE - 1) & ~(CacheLineSize - 1);
61+
constexpr uint32_t CacheSize = RANDOMX_ARGON_MEMORY * ArgonBlockSize;
62+
constexpr uint64_t DatasetSize = RANDOMX_DATASET_BASE_SIZE + RANDOMX_DATASET_EXTRA_SIZE;
63+
constexpr uint32_t DatasetExtraItems = RANDOMX_DATASET_EXTRA_SIZE / RANDOMX_DATASET_ITEM_SIZE;
6164

6265
#ifdef TRACE
6366
constexpr bool trace = true;

src/configuration.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,11 @@ along with RandomX. If not, see<http://www.gnu.org/licenses/>.
3737
#define RANDOMX_SUPERSCALAR_LATENCY 170
3838
#define RANDOMX_SUPERSCALAR_MAX_SIZE 512
3939

40-
//Dataset size in bytes. Must be a power of 2.
41-
#define RANDOMX_DATASET_SIZE (2ULL * 1024 * 1024 * 1024)
40+
//Dataset base size in bytes. Must be a power of 2.
41+
#define RANDOMX_DATASET_BASE_SIZE (2ULL * 1024 * 1024 * 1024)
42+
43+
//Dataset extra size. Must be divisible by 64.
44+
#define RANDOMX_DATASET_EXTRA_SIZE 33554368
4245

4346
//Number of instructions in a RandomX program
4447
#define RANDOMX_PROGRAM_SIZE 256

src/dataset.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ randomx_dataset::~randomx_dataset() {
4545
}
4646

4747
static_assert(RANDOMX_ARGON_MEMORY % (RANDOMX_ARGON_LANES * ARGON2_SYNC_POINTS) == 0, "RANDOMX_ARGON_MEMORY - invalid value");
48+
static_assert(ARGON2_BLOCK_SIZE == randomx::ArgonBlockSize, "Unpexpected value of ARGON2_BLOCK_SIZE");
4849

4950
void randomx_cache::initialize(const void *seed, size_t seedSize) {
5051
uint32_t memory_blocks, segment_length;
@@ -117,22 +118,22 @@ namespace randomx {
117118

118119
template<class Allocator>
119120
void Dataset<Allocator>::allocate() {
120-
memory = (uint8_t*)Allocator::allocMemory(RANDOMX_DATASET_SIZE);
121+
memory = (uint8_t*)Allocator::allocMemory(DatasetSize);
121122
}
122123

123124
template<class Allocator>
124125
Dataset<Allocator>::~Dataset() {
125-
Allocator::freeMemory(memory, RANDOMX_DATASET_SIZE);
126+
Allocator::freeMemory(memory, DatasetSize);
126127
}
127128

128129
template<class Allocator>
129130
void Cache<Allocator>::allocate() {
130-
memory = (uint8_t*)Allocator::allocMemory(RANDOMX_ARGON_MEMORY * ARGON2_BLOCK_SIZE);
131+
memory = (uint8_t*)Allocator::allocMemory(CacheSize);
131132
}
132133

133134
template<class Allocator>
134135
Cache<Allocator>::~Cache() {
135-
Allocator::freeMemory(memory, RANDOMX_ARGON_MEMORY * ARGON2_BLOCK_SIZE);
136+
Allocator::freeMemory(memory, CacheSize);
136137
}
137138

138139
template<class Allocator>
@@ -160,16 +161,16 @@ namespace randomx {
160161
template class CacheWithJit<LargePageAllocator>;
161162

162163
constexpr uint64_t superscalarMul0 = 6364136223846793005ULL;
163-
constexpr uint64_t superscalarAdd1 = 9298410992540426748ULL;
164+
constexpr uint64_t superscalarAdd1 = 9298411001130361340ULL;
164165
constexpr uint64_t superscalarAdd2 = 12065312585734608966ULL;
165-
constexpr uint64_t superscalarAdd3 = 9306329213124610396ULL;
166+
constexpr uint64_t superscalarAdd3 = 9306329213124626780ULL;
166167
constexpr uint64_t superscalarAdd4 = 5281919268842080866ULL;
167168
constexpr uint64_t superscalarAdd5 = 10536153434571861004ULL;
168169
constexpr uint64_t superscalarAdd6 = 3398623926847679864ULL;
169170
constexpr uint64_t superscalarAdd7 = 9549104520008361294ULL;
170171

171172
static inline uint8_t* getMixBlock(uint64_t registerValue, uint8_t *memory) {
172-
constexpr uint32_t mask = (RANDOMX_ARGON_MEMORY * ArgonBlockSize / CacheLineSize - 1);
173+
constexpr uint32_t mask = CacheSize / CacheLineSize - 1;
173174
return memory + (registerValue & mask) * CacheLineSize;
174175
}
175176

src/jit_compiler_x86.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ namespace randomx {
217217
static const uint8_t RET = 0xc3;
218218
static const uint8_t LEA_32[] = { 0x67, 0x41, 0x8d };
219219
static const uint8_t MOVNTI[] = { 0x4c, 0x0f, 0xc3 };
220+
static const uint8_t ADD_EBX_I[] = { 0x81, 0xc3 };
220221

221222
static const uint8_t NOP1[] = { 0x90 };
222223
static const uint8_t NOP2[] = { 0x66, 0x90 };
@@ -250,9 +251,11 @@ namespace randomx {
250251
generateProgramEpilogue(prog);
251252
}
252253

253-
void JitCompilerX86::generateProgramLight(Program& prog, ProgramConfiguration& pcfg) {
254+
void JitCompilerX86::generateProgramLight(Program& prog, ProgramConfiguration& pcfg, uint32_t datasetOffset) {
254255
generateProgramPrologue(prog, pcfg);
255256
emit(codeReadDatasetLightSshInit, readDatasetLightInitSize);
257+
emit(ADD_EBX_I);
258+
emit32(datasetOffset / CacheLineSize);
256259
emitByte(CALL);
257260
emit32(superScalarHashOffset - (codePos + 4));
258261
emit(codeReadDatasetLightSshFin, readDatasetLightFinSize);

src/jit_compiler_x86.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ namespace randomx {
4141
JitCompilerX86();
4242
~JitCompilerX86();
4343
void generateProgram(Program&, ProgramConfiguration&);
44-
void generateProgramLight(Program&, ProgramConfiguration&);
44+
void generateProgramLight(Program&, ProgramConfiguration&, uint32_t);
4545
template<size_t N>
4646
void generateSuperscalarHash(SuperscalarProgram (&programs)[N], std::vector<uint64_t> &);
4747
void generateDatasetInitCode();

src/randomx.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ extern "C" {
9191
}
9292

9393
unsigned long randomx_dataset_item_count() {
94-
return RANDOMX_DATASET_SIZE / RANDOMX_DATASET_ITEM_SIZE;
94+
return randomx::DatasetSize / RANDOMX_DATASET_ITEM_SIZE;
9595
}
9696

9797
void randomx_init_dataset(randomx_dataset *dataset, randomx_cache *cache, unsigned long startItem, unsigned long itemCount) {

src/tests/benchmark.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ int main(int argc, char** argv) {
225225
std::cout << "Calculated result: ";
226226
result.print(std::cout);
227227
if (noncesCount == 1000 && seedValue == 0)
228-
std::cout << "Reference result: b69741719152625854031c2337ceae68c3030f2b9581a73acebaa69fc9b555fc" << std::endl;
228+
std::cout << "Reference result: 918a8bc3ce0e537eec9d3c5e1a8bb3204ae3954f14c50c14810b38e49588a9e0" << std::endl;
229229
if (!miningMode) {
230230
std::cout << "Performance: " << 1000 * elapsed / noncesCount << " ms per hash" << std::endl;
231231
}

src/tests/superscalar-init.cpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,25 @@ along with RandomX. If not, see<http://www.gnu.org/licenses/>.
2424
#include "../superscalar.hpp"
2525
#include "../common.hpp"
2626

27-
const uint8_t seed[32] = { 191, 182, 222, 175, 249, 89, 134, 104, 241, 68, 191, 62, 162, 166, 61, 64, 123, 191, 227, 193, 118, 60, 188, 53, 223, 133, 175, 24, 123, 230, 55, 74 };
28-
2927
int main() {
30-
std::cout << "THIS PROGRAM REQUIRES MORE THAN 10 GB OF RAM TO COMPLETE" << std::endl;
28+
std::cout << "THIS PROGRAM REQUIRES MORE THAN 16 GB OF RAM TO COMPLETE" << std::endl;
3129
std::vector<uint64_t> dummy;
3230
constexpr uint64_t superscalarMul0 = 6364136223846793005ULL;
33-
constexpr uint64_t superscalarAdd1 = 9298410992540426748ULL; //9298410992540426048ULL
31+
constexpr uint64_t superscalarAdd1 = 0x810A978A59F5A1FC; //9298410992540426748ULL; //9298410992540426048ULL
3432
constexpr uint64_t superscalarAdd2 = 12065312585734608966ULL;
35-
constexpr uint64_t superscalarAdd3 = 9306329213124610396ULL;
33+
constexpr uint64_t superscalarAdd3 = 0x8126B91CBF22495C; //9306329213124610396ULL;
3634
constexpr uint64_t superscalarAdd4 = 5281919268842080866ULL;
3735
constexpr uint64_t superscalarAdd5 = 10536153434571861004ULL;
3836
constexpr uint64_t superscalarAdd6 = 3398623926847679864ULL;
3937
constexpr uint64_t superscalarAdd7 = 9549104520008361294ULL;
40-
constexpr uint32_t totalBlocks = RANDOMX_DATASET_SIZE / randomx::CacheLineSize;
38+
constexpr uint32_t totalItems = randomx::DatasetSize / randomx::CacheLineSize;
4139
std::unordered_set<uint64_t> registerValues;
42-
registerValues.reserve(totalBlocks);
43-
registerValues.rehash(totalBlocks);
40+
registerValues.reserve(totalItems);
41+
registerValues.rehash(totalItems);
4442
int collisionCount[9] = { 0 };
45-
for (uint32_t blockNumber = 0; blockNumber < totalBlocks; ++blockNumber) {
43+
for (uint32_t itemNumber = 0; itemNumber < totalItems; ++itemNumber) {
4644
uint64_t rl[8];
47-
rl[0] = (blockNumber + 1) * superscalarMul0;
45+
rl[0] = (itemNumber + 1) * superscalarMul0;
4846
rl[1] = rl[0] ^ superscalarAdd1;
4947
rl[2] = rl[0] ^ superscalarAdd2;
5048
rl[3] = rl[0] ^ superscalarAdd3;
@@ -57,19 +55,19 @@ int main() {
5755
uint64_t reducedValue = rl[i] & 0x3FFFFFFFFFFFF8; //bits 3-53 only
5856
if (registerValues.find(reducedValue) != registerValues.end()) {
5957
blockCollisions++;
60-
std::cout << "Block " << blockNumber << ": collision of register r" << i << std::endl;
58+
std::cout << "Item " << itemNumber << ": collision of register r" << i << std::endl;
6159
}
6260
else {
6361
registerValues.insert(reducedValue);
6462
}
6563
}
6664
collisionCount[blockCollisions]++;
67-
if ((blockNumber % (320 * 1024)) == 0)
68-
std::cout << "Block " << blockNumber << " processed" << std::endl;
65+
if ((itemNumber % (320 * 1024)) == 0)
66+
std::cout << "Item " << itemNumber << " processed" << std::endl;
6967
}
7068

7169
for (int i = 0; i < 9; ++i) {
72-
std::cout << i << " register(s) collide in " << collisionCount[i] << " blocks" << std::endl;
70+
std::cout << i << " register(s) collide in " << collisionCount[i] << " items" << std::endl;
7371
}
7472

7573
return 0;

src/virtual_machine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ void randomx_vm::initialize() {
6969
config.readReg2 = 4 + (addressRegisters & 1);
7070
addressRegisters >>= 1;
7171
config.readReg3 = 6 + (addressRegisters & 1);
72-
//datasetBase = program.getEntropy(13) % datasetRange;
72+
datasetOffset = (program.getEntropy(13) & randomx::DatasetExtraItems) * randomx::CacheLineSize;
7373
constexpr uint64_t mask22bit = (1ULL << 22) - 1;
7474
constexpr uint64_t maskExp240 = ieee_get_exponent_mask<-240>();
7575
store64(&config.eMask[0], (program.getEntropy(14) & mask22bit) | maskExp240);

src/virtual_machine.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class randomx_vm {
4444
alignas(16) randomx::ProgramConfiguration config;
4545
randomx::MemoryRegisters mem;
4646
uint8_t* scratchpad;
47+
uint8_t* datasetBasePtr;
48+
uint32_t datasetOffset;
4749
};
4850

4951
namespace randomx {

src/vm_compiled.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ namespace randomx {
2828
template<class Allocator, bool softAes>
2929
void CompiledVm<Allocator, softAes>::setDataset(randomx_dataset* dataset) {
3030
mem.memory = dataset->memory;
31-
//datasetBasePtr = dataset.memory;
31+
datasetBasePtr = dataset->memory;
3232
}
3333

3434
template<class Allocator, bool softAes>
3535
void CompiledVm<Allocator, softAes>::run(void* seed) {
3636
VmBase<Allocator, softAes>::generateProgram(seed);
3737
randomx_vm::initialize();
3838
compiler.generateProgram(program, config);
39-
//mem.memory = datasetBasePtr + (datasetBase * CacheLineSize);
39+
mem.memory = datasetBasePtr + datasetOffset;
4040
execute();
4141
}
4242

src/vm_compiled.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,12 @@ namespace randomx {
4848
using VmBase<Allocator, softAes>::config;
4949
using VmBase<Allocator, softAes>::reg;
5050
using VmBase<Allocator, softAes>::scratchpad;
51+
using VmBase<Allocator, softAes>::datasetBasePtr;
52+
using VmBase<Allocator, softAes>::datasetOffset;
5153
protected:
5254
void execute();
5355

5456
JitCompilerX86 compiler;
55-
uint8_t* datasetBasePtr;
5657
};
5758

5859
using CompiledVmDefault = CompiledVm<AlignedAllocator<CacheLineSize>, true>;

src/vm_compiled_light.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,13 @@ namespace randomx {
2727
void CompiledLightVm<Allocator, softAes>::setCache(randomx_cache* cache) {
2828
mem.memory = cache->memory;
2929
compiler.generateSuperscalarHash(cache->programs, cache->reciprocalCache);
30-
//datasetBasePtr = ds.dataset.memory;
3130
}
3231

3332
template<class Allocator, bool softAes>
3433
void CompiledLightVm<Allocator, softAes>::run(void* seed) {
3534
VmBase<Allocator, softAes>::generateProgram(seed);
3635
randomx_vm::initialize();
37-
compiler.generateProgramLight(program, config);
38-
//mem.memory = datasetBasePtr + (datasetBase * CacheLineSize);
36+
compiler.generateProgramLight(program, config, datasetOffset);
3937
CompiledVm<Allocator, softAes>::execute();
4038
}
4139

src/vm_compiled_light.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ namespace randomx {
4444
using CompiledVm<Allocator, softAes>::compiler;
4545
using CompiledVm<Allocator, softAes>::program;
4646
using CompiledVm<Allocator, softAes>::config;
47+
using CompiledVm<Allocator, softAes>::datasetOffset;
4748
};
4849

4950
using CompiledLightVmDefault = CompiledLightVm<AlignedAllocator<CacheLineSize>, true>;

src/vm_interpreted.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ namespace randomx {
331331

332332
mem.mx ^= r[config.readReg2] ^ r[config.readReg3];
333333
mem.mx &= CacheLineAlignMask;
334-
datasetRead(mem.ma, r);
334+
datasetRead(datasetOffset + mem.ma, r);
335335
std::swap(mem.mx, mem.ma);
336336

337337
if (trace) {

src/vm_interpreted.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ namespace randomx {
5757
using VmBase<Allocator, softAes>::program;
5858
using VmBase<Allocator, softAes>::config;
5959
using VmBase<Allocator, softAes>::reg;
60+
using VmBase<Allocator, softAes>::datasetBasePtr;
61+
using VmBase<Allocator, softAes>::datasetOffset;
6062
void* operator new(size_t size) {
6163
void* ptr = AlignedAllocator<CacheLineSize>::allocMemory(size);
6264
if (ptr == nullptr)

src/vm_interpreted_light.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ namespace randomx {
2525
template<class Allocator, bool softAes>
2626
void InterpretedLightVm<Allocator, softAes>::setCache(randomx_cache* cache) {
2727
mem.memory = cache->memory;
28-
//datasetRange = (size - RANDOMX_DATASET_SIZE + CacheLineSize) / CacheLineSize;
2928
cachePtr = cache;
3029
}
3130

0 commit comments

Comments
 (0)