Skip to content

Commit 87b8253

Browse files
committed
API documentation
'dataset block' -> 'dataset item' customizable benchmark seed
1 parent bc78b62 commit 87b8253

8 files changed

+136
-27
lines changed

src/common.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ namespace randomx {
5959
constexpr uint32_t CacheLineAlignMask = (RANDOMX_DATASET_SIZE - 1) & ~(CacheLineSize - 1);
6060
constexpr uint32_t CacheSize = RANDOMX_ARGON_MEMORY * 1024;
6161

62-
static_assert(RANDOMX_DATASET_BLOCKS == RANDOMX_DATASET_SIZE / CacheLineSize, "Invalid value of RANDOMX_DATASET_BLOCKS");
62+
static_assert(RANDOMX_DATASET_ITEMS == RANDOMX_DATASET_SIZE / CacheLineSize, "Invalid value of RANDOMX_DATASET_ITEMS");
6363

6464
#ifdef TRACE
6565
constexpr bool trace = true;

src/dataset.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,11 @@ namespace randomx {
180180
return memory + (registerValue & mask) * CacheLineSize;
181181
}
182182

183-
void initDatasetBlock(randomx_cache* cache, uint8_t* out, uint64_t blockNumber) {
183+
void initDatasetItem(randomx_cache* cache, uint8_t* out, uint64_t itemNumber) {
184184
int_reg_t rl[8];
185185
uint8_t* mixBlock;
186-
uint64_t registerValue = blockNumber;
187-
rl[0] = (blockNumber + 1) * superscalarMul0;
186+
uint64_t registerValue = itemNumber;
187+
rl[0] = (itemNumber + 1) * superscalarMul0;
188188
rl[1] = rl[0] ^ superscalarAdd1;
189189
rl[2] = rl[0] ^ superscalarAdd2;
190190
rl[3] = rl[0] ^ superscalarAdd3;
@@ -207,8 +207,8 @@ namespace randomx {
207207
memcpy(out, &rl, CacheLineSize);
208208
}
209209

210-
void initDataset(randomx_cache* cache, uint8_t* dataset, uint32_t startBlock, uint32_t endBlock) {
211-
for (uint32_t blockNumber = startBlock; blockNumber < endBlock; ++blockNumber, dataset += CacheLineSize)
212-
initDatasetBlock(cache, dataset, blockNumber);
210+
void initDataset(randomx_cache* cache, uint8_t* dataset, uint32_t startItem, uint32_t endItem) {
211+
for (uint32_t itemNumber = startItem; itemNumber < endItem; ++itemNumber, dataset += CacheLineSize)
212+
initDatasetItem(cache, dataset, itemNumber);
213213
}
214214
}

src/dataset.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,6 @@ namespace randomx {
7373
using CacheLargePage = Cache<LargePageAllocator>;
7474
using CacheWithJitLargePage = CacheWithJit<LargePageAllocator>;
7575

76-
void initDatasetBlock(randomx_cache* cache, uint8_t* out, uint64_t blockNumber);
76+
void initDatasetItem(randomx_cache* cache, uint8_t* out, uint64_t blockNumber);
7777
void initDataset(randomx_cache* cache, uint8_t* dataset, uint32_t startBlock, uint32_t endBlock);
7878
}

src/randomx.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ extern "C" {
9090
return dataset;
9191
}
9292

93-
void randomx_init_dataset(randomx_dataset *dataset, randomx_cache *cache, unsigned long startBlock, unsigned long blockCount) {
93+
void randomx_init_dataset(randomx_dataset *dataset, randomx_cache *cache, unsigned long startItem, unsigned long itemCount) {
9494
randomx::DatasetInitFunc dsfunc = cache->getInitFunc();
95-
dsfunc(cache, dataset->memory + startBlock * randomx::CacheLineSize, startBlock, startBlock + blockCount);
95+
dsfunc(cache, dataset->memory + startItem * randomx::CacheLineSize, startItem, startItem + itemCount);
9696
}
9797

9898
void randomx_release_dataset(randomx_dataset *dataset) {

src/randomx.h

+110-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ along with RandomX. If not, see<http://www.gnu.org/licenses/>.
2323
#include <stddef.h>
2424

2525
#define RANDOMX_HASH_SIZE 32
26-
#define RANDOMX_DATASET_BLOCKS 33554432UL
26+
#define RANDOMX_DATASET_ITEMS 33554432UL
2727

2828
typedef enum {
2929
RANDOMX_FLAG_DEFAULT = 0,
@@ -41,19 +41,127 @@ typedef struct randomx_vm randomx_vm;
4141
extern "C" {
4242
#endif
4343

44+
/**
45+
* Creates a randomx_cache structure and allocates memory for RandomX Cache.
46+
*
47+
* @param flags is any combination of these 2 flags (each flag can be set or not set):
48+
* RANDOMX_FLAG_LARGE_PAGES - allocate memory in large pages
49+
* RANDOMX_FLAG_JIT - create cache structure with JIT compilation support; this makes
50+
* subsequent Dataset initialization faster
51+
*
52+
* @return Pointer to an allocated randomx_cache structure.
53+
NULL is returned if memory allocation fails or if the RANDOMX_FLAG_JIT
54+
is set and JIT compilation is not supported on the current platform.
55+
*/
4456
randomx_cache *randomx_alloc_cache(randomx_flags flags);
57+
58+
/**
59+
* Initializes the cache memory and SuperscalarHash using the provided seed value.
60+
*
61+
* @param cache is a pointer to a previously allocated randomx_cache structure. Must not be NULL.
62+
* @param seed is a pointer to memory which contains the seed value. Must not be NULL.
63+
* @param seedSize is the number of bytes of the seed.
64+
*/
4565
void randomx_init_cache(randomx_cache *cache, const void *seed, size_t seedSize);
66+
67+
/**
68+
* Releases all memory occupied by the randomx_cache structure.
69+
*
70+
* @param cache is a pointer to a previously allocated randomx_cache structure.
71+
*/
4672
void randomx_release_cache(randomx_cache* cache);
4773

74+
/**
75+
* Creates a randomx_dataset structure and allocates memory for RandomX Dataset.
76+
*
77+
* @param flags is the initialization flags. Only one flag is supported (can be set or not set):
78+
* RANDOMX_FLAG_LARGE_PAGES - allocate memory in large pages
79+
80+
* @return Pointer to an allocated randomx_cache structure.
81+
NULL is returned if memory allocation fails.
82+
*/
4883
randomx_dataset *randomx_alloc_dataset(randomx_flags flags);
49-
void randomx_init_dataset(randomx_dataset *dataset, randomx_cache *cache, unsigned long startBlock, unsigned long blockCount);
84+
85+
/**
86+
* Initializes dataset items.
87+
*
88+
* Note: In order to use the Dataset, all items from 0 to (RANDOMX_DATASET_ITEMS - 1) must be initialized.
89+
* This may be done by several calls to this function using non-overlapping item sequences.
90+
*
91+
* @param dataset is a pointer to a previously allocated randomx_dataset structure. Must not be NULL.
92+
* @param cache is a pointer to a previously allocated and initialized randomx_cache structure. Must not be NULL.
93+
* @param startItem is the item number where intialization should start.
94+
* @param itemCount is the number of items that should be initialized.
95+
*/
96+
void randomx_init_dataset(randomx_dataset *dataset, randomx_cache *cache, unsigned long startItem, unsigned long itemCount);
97+
98+
/**
99+
* Releases all memory occupied by the randomx_dataset structure.
100+
*
101+
* @param dataset is a pointer to a previously allocated randomx_dataset structure.
102+
*/
50103
void randomx_release_dataset(randomx_dataset *dataset);
51104

105+
/**
106+
* Creates and initializes a RandomX virtual machine.
107+
*
108+
* @param flags is any combination of these 4 flags (each flag can be set or not set):
109+
* RANDOMX_FLAG_LARGE_PAGES - allocate scratchpad memory in large pages
110+
* RANDOMX_FLAG_HARD_AES - virtual machine will use hardware accelerated AES
111+
* RANDOMX_FLAG_FULL_MEM - virtual machine will use the full dataset
112+
* RANDOMX_FLAG_JIT - virtual machine will use a JIT compiler
113+
* The numeric values of the flags are ordered so that a higher value will provide
114+
* faster hash calculation and a lower numeric value will provide higher portability.
115+
* Using RANDOMX_FLAG_DEFAULT (all flags not set) works on all platforms, but is the slowest.
116+
* @param cache is a pointer to an initialized randomx_cache structure. Can be
117+
* NULL if RANDOMX_FLAG_FULL_MEM is set.
118+
* @param dataset is a pointer to a randomx_dataset structure. Can be NULL
119+
* if RANDOMX_FLAG_FULL_MEM is not set.
120+
*
121+
* @return Pointer to an initialized randomx_vm structure.
122+
* Returns NULL if:
123+
* (1) Scratchpad memory allocation fails.
124+
* (2) The requested initialization flags are not supported on the current platform.
125+
* (3) cache parameter is NULL and RANDOMX_FLAG_FULL_MEM is not set
126+
* (4) dataset parameter is NULL and RANDOMX_FLAG_FULL_MEM is set
127+
*/
52128
randomx_vm *randomx_create_vm(randomx_flags flags, randomx_cache *cache, randomx_dataset *dataset);
129+
130+
/**
131+
* Reinitializes a virtual machine with a new Cache. This function should be called anytime
132+
* the Cache is reinitialized with a new seed.
133+
*
134+
* @param machine is a pointer to a randomx_vm structure that was initialized
135+
* without RANDOMX_FLAG_FULL_MEM. Must not be NULL.
136+
* @param cache is a pointer to an initialized randomx_cache structure. Must not be NULL.
137+
*/
53138
void randomx_vm_set_cache(randomx_vm *machine, randomx_cache* cache);
139+
140+
/**
141+
* Reinitializes a virtual machine with a new Dataset.
142+
*
143+
* @param machine is a pointer to a randomx_vm structure that was initialized
144+
* with RANDOMX_FLAG_FULL_MEM. Must not be NULL.
145+
* @param dataset is a pointer to an initialized randomx_dataset structure. Must not be NULL.
146+
*/
54147
void randomx_vm_set_dataset(randomx_vm *machine, randomx_dataset *dataset);
148+
149+
/**
150+
* Releases all memory occupied by the randomx_vm structure.
151+
*
152+
* @param machine is a pointer to a previously created randomx_vm structure.
153+
*/
55154
void randomx_destroy_vm(randomx_vm *machine);
56155

156+
/**
157+
* Calculates a RandomX hash value.
158+
*
159+
* @param machine is a pointer to a randomx_vm structure. Must not be NULL.
160+
* @param input is a pointer to memory to be hashed. Must not be NULL.
161+
* @param inputSize is the number of bytes to be hashed.
162+
* @param output is a pointer to memory where the hash will be stored. Must not
163+
* be NULL and at least RANDOMX_HASH_SIZE bytes must be available for writing.
164+
*/
57165
void randomx_calculate_hash(randomx_vm *machine, const void *input, size_t inputSize, void *output);
58166

59167
#if defined(__cplusplus)

src/tests/api-example2.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ int main() {
2020
std::cout << "Dataset allocation failed" << std::endl;
2121
return 1;
2222
}
23-
std::thread t1(&randomx_init_dataset, myDataset, myCache, 0, RANDOMX_DATASET_BLOCKS / 2);
24-
std::thread t2(&randomx_init_dataset, myDataset, myCache, RANDOMX_DATASET_BLOCKS / 2, RANDOMX_DATASET_BLOCKS / 2);
23+
std::thread t1(&randomx_init_dataset, myDataset, myCache, 0, RANDOMX_DATASET_ITEMS / 2);
24+
std::thread t2(&randomx_init_dataset, myDataset, myCache, RANDOMX_DATASET_ITEMS / 2, RANDOMX_DATASET_ITEMS / 2);
2525
t1.join();
2626
t2.join();
2727
randomx_release_cache(myCache);

src/tests/benchmark.cpp

+12-11
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ along with RandomX. If not, see<http://www.gnu.org/licenses/>.
3030
#include "../randomx.h"
3131
#include "../blake2/endian.h"
3232

33-
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 };
34-
3533
const uint8_t blockTemplate_[] = {
3634
0x07, 0x07, 0xf7, 0xa4, 0xf0, 0xd6, 0x05, 0xb3, 0x03, 0x26, 0x08, 0x16, 0xba, 0x3f, 0x10, 0x90, 0x2e, 0x1a, 0x14,
3735
0x5a, 0xc5, 0xfa, 0xd3, 0xaa, 0x3a, 0xf6, 0xea, 0x44, 0xc1, 0x18, 0x69, 0xdc, 0x4f, 0x85, 0x3f, 0x00, 0x2b, 0x2e,
@@ -82,6 +80,7 @@ void printUsage(const char* executable) {
8280
std::cout << " --threads T use T threads (default: 1)" << std::endl;
8381
std::cout << " --init Q initialize dataset with Q threads (default: 1)" << std::endl;
8482
std::cout << " --nonces N run N nonces (default: 1000)" << std::endl;
83+
std::cout << " --seed S seed for cache initialization (default: 0)" << std::endl;
8584
}
8685

8786
void mine(randomx_vm* vm, std::atomic<uint32_t>& atomicNonce, AtomicHash& result, uint32_t noncesCount, int thread) {
@@ -102,13 +101,15 @@ void mine(randomx_vm* vm, std::atomic<uint32_t>& atomicNonce, AtomicHash& result
102101
int main(int argc, char** argv) {
103102
bool softAes, miningMode, verificationMode, help, largePages, jit;
104103
int noncesCount, threadCount, initThreadCount;
104+
int32_t seed;
105105

106106
readOption("--softAes", argc, argv, softAes);
107107
readOption("--mine", argc, argv, miningMode);
108108
readOption("--verify", argc, argv, verificationMode);
109109
readIntOption("--threads", argc, argv, threadCount, 1);
110110
readIntOption("--nonces", argc, argv, noncesCount, 1000);
111111
readIntOption("--init", argc, argv, initThreadCount, 1);
112+
readIntOption("--seed", argc, argv, seed, 0);
112113
readOption("--largePages", argc, argv, largePages);
113114
readOption("--jit", argc, argv, jit);
114115
readOption("--help", argc, argv, help);
@@ -172,28 +173,28 @@ int main(int argc, char** argv) {
172173
std::cout << "ERROR: Cache allocation failed" << std::endl;
173174
return 1;
174175
}
175-
randomx_init_cache(cache, seed, sizeof(seed));
176+
randomx_init_cache(cache, &seed, sizeof(seed));
176177
if (miningMode) {
177178
dataset = randomx_alloc_dataset(flags);
178179
if (dataset == nullptr) {
179180
std::cout << "ERROR: Dataset allocation failed" << std::endl;
180181
return 1;
181182
}
182183
if (initThreadCount > 1) {
183-
auto perThread = RANDOMX_DATASET_BLOCKS / initThreadCount;
184-
auto remainder = RANDOMX_DATASET_BLOCKS % initThreadCount;
185-
uint32_t startBlock = 0;
184+
auto perThread = RANDOMX_DATASET_ITEMS / initThreadCount;
185+
auto remainder = RANDOMX_DATASET_ITEMS % initThreadCount;
186+
uint32_t startItem = 0;
186187
for (int i = 0; i < initThreadCount; ++i) {
187188
auto count = perThread + (i == initThreadCount - 1 ? remainder : 0);
188-
threads.push_back(std::thread(&randomx_init_dataset, dataset, cache, startBlock, count));
189-
startBlock += count;
189+
threads.push_back(std::thread(&randomx_init_dataset, dataset, cache, startItem, count));
190+
startItem += count;
190191
}
191192
for (unsigned i = 0; i < threads.size(); ++i) {
192193
threads[i].join();
193194
}
194195
}
195196
else {
196-
randomx_init_dataset(dataset, cache, 0, RANDOMX_DATASET_BLOCKS);
197+
randomx_init_dataset(dataset, cache, 0, RANDOMX_DATASET_ITEMS);
197198
}
198199
randomx_release_cache(cache);
199200
threads.clear();
@@ -227,8 +228,8 @@ int main(int argc, char** argv) {
227228
double elapsed = sw.getElapsed();
228229
std::cout << "Calculated result: ";
229230
result.print(std::cout);
230-
if (noncesCount == 1000)
231-
std::cout << "Reference result: dc34604eed2fbba0e8fae26b2270b90d8aad9466ba39950fd8904248442e850a" << std::endl;
231+
if (noncesCount == 1000 && seed == 0)
232+
std::cout << "Reference result: b69741719152625854031c2337ceae68c3030f2b9581a73acebaa69fc9b555fc" << std::endl;
232233
if (!miningMode) {
233234
std::cout << "Performance: " << 1000 * elapsed / noncesCount << " ms per hash" << std::endl;
234235
}

src/vm_interpreted_light.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ namespace randomx {
3131

3232
template<class Allocator, bool softAes>
3333
void InterpretedLightVm<Allocator, softAes>::datasetRead(uint32_t address, int_reg_t(&r)[8]) {
34-
uint32_t blockNumber = address / CacheLineSize;
34+
uint32_t itemNumber = address / CacheLineSize;
3535
int_reg_t rl[8];
3636

37-
initDatasetBlock(cachePtr, (uint8_t*)rl, blockNumber);
37+
initDatasetItem(cachePtr, (uint8_t*)rl, itemNumber);
3838

3939
for (unsigned q = 0; q < 8; ++q)
4040
r[q] ^= rl[q];

0 commit comments

Comments
 (0)