Skip to content

Commit

Permalink
adding --logEvery switch; re-enabled writing out of asm's
Browse files Browse the repository at this point in the history
  • Loading branch information
dderjoel committed Oct 6, 2023
1 parent 5a9130c commit f250ffd
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 42 deletions.
3 changes: 2 additions & 1 deletion First_Steps.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ Parameter | default | possible / typical values | description
--betRatio | 0.2 | 0.1, 0.3 | The share from parameter `--evals`, which are spent for all bets, in per cent (i.e. 0.2 means 20% of --evals will be used for the bet part, and 80% for the final run-part)
--resultDir | ./results | /tmp/myresults | The directory under which `<BRIDGE>/<SYMBOL>` will be created and the result files will be stored
--no-proof | | --no-proof, --proof | [dis\|en]ables the Fiat-Proofing system. It is enabled by default for `fiat`-bridge, disabled for the rest.
--memoryConstraints| none | none, all, out1-arg1 | none: any argN[n] can be read after any outN[n] as been written. That is okay, if all memoy is distinct. 'out1-arg1' specifies that arg1[n] cannnot be read, if out1[n] has been written. It may read arg1[n+m] after out1[n] has been written. Use that if you want to call `mul(r,r,x)` or `sq(a,a)`. all: no argN[n] can be read if any outN[n] has been written. Use that if memory can be overlapping and unaligned.
--memoryConstraints| none | none, all, out1-arg1 | none: any argN[n] can be read after any outN[n] as been written. That is okay, if all memory is distinct. 'out1-arg1' specifies that arg1[n] cannot be read, if out1[n] has been written. It may read arg1[n+m] after out1[n] has been written. Use that if you want to call `mul(r,r,x)` or `sq(a,a)`. all: no argN[n] can be read if any outN[n] has been written. Use that if memory can be overlapping and unaligned.
--logEvery | 5 | 10, 50, ... | This number determines after how many per cent a line is logged during the run-optimisation. Note, that with every line, an Assembly File will be written to disk. Also note, that this does not apply to the Bet-parts, i.e. each bet will only have two lines/files written, at 50%, and 100%.

For more information check `./CryptOpt --help`

Expand Down
1 change: 1 addition & 0 deletions completion/_cryptopt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ _arguments -S -s \
'(-b --bets -r --betRatio)--single[no bets. Short for --bets=1 --betRatio=1]:single:' \
'(-b --bets)'{-b,--bets=}'[number of Bets]:bets:' \
'(-r --betRatio)'{-r,--betRatio=}'[Ratio of how many evals to spend on bets.]:br:' \
'(--logEvery)'--logEvery='[In per cent; a line is logged during the run-optimisation and an Assembly File written.]:logEvery:' \
'--cyclegoal=[Number of cycles to measure, adjusts batch size dynamically]:cg:' \
'--logComment=[Hint displayed during optimization]:lc:' \
'(--no-proof --proof)'{--no-proof,--proof}'[Dis-/En ables proofing. Default on with fiat, default off elsewhere]:proof:' \
Expand Down
1 change: 1 addition & 0 deletions src/CryptOpt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ async function allBets(evals: number, bets: number): Promise<RunResult[]> {
...parsedArgs,
evals,
logComment: `${parsedArgs.logComment} ${i}/${bets}`,
logEvery: 50,
seed: derivedSeed,
};
Logger.log("running a bet with " + JSON.stringify(args, undefined, 2));
Expand Down
5 changes: 5 additions & 0 deletions src/helper/argParse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ export const parsedArgs = y
default: "",
describe: "May provide a hint of any kind to be printed on the status line",
})
.option("logEvery", {
number: true,
default: 5,
describe: "Print out a line break and write out an asm to the results folder. In Per Cent.",
})
.option("cyclegoal", {
number: true,
default: 10000,
Expand Down
4 changes: 1 addition & 3 deletions src/helper/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ export const DONT_CARE_IMM_VALS = [

export const TEMP_VAR_START_NUM = 10000;
export const TEMP_VAR_END_NUM = 99999;
export const PRINT_EVERY = 10; // this is to have the correct scaling in the graph
export const LOG_EVERY = 20; // log line (to screen and file) every 20 percent, if this var it set to 20, I guess must be a multple of PRINTevery
// the higher that number, the mire logs.
export const PRINT_EVERY = 10; // this is to have the correct scaling in the graph; every this much mutations, a line is updated on the screen, and an entry in the csv.

export const ADX = {
[Flags.CF]: "adcx",
Expand Down
70 changes: 36 additions & 34 deletions src/optimizer/optimizer.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import { errorOut, ERRORS } from "@/errors";
import {
analyseMeasureResult,
generateResultFilename,
LOG_EVERY,
padSeed,
PRINT_EVERY,
shouldProof,
Expand Down Expand Up @@ -152,7 +151,8 @@ export class Optimizer {
let show_per_second = "many/s";
let per_second_counter = 0;
const intervalHandle = setInterval(() => {
if (numEvals > 0) {
// Increase Number of evaluations taken.
if (numEvals !== 0) {
// not first eval, thus we want to mutate.
this.mutate();
}
Expand Down Expand Up @@ -295,11 +295,18 @@ export class Optimizer {
}

logMutation({ choice, kept, numEvals });
if (numEvals % PRINT_EVERY == 0) {
// print every 10th eval
// a line every 5% (also to logfile) also write the asm when
const writeout = numEvals % (this.args.evals / LOG_EVERY) === 0;

const [asmFile, mutationsCsvFile] = generateResultFilename(
{ ...this.args, symbolname: this.symbolname },
[`_ratio${ratioString.replace(".", "")}.asm`, `.csv`],
);

const writeout =
// print every whatever percent;
((100 * numEvals) / this.args.evals) % this.args.logEvery === 0 ||
// or at the end
numEvals == this.args.evals;

if (numEvals % PRINT_EVERY === 0 || writeout) {
const statusline = genStatusLine({
...this.args,
analyseResult,
Expand All @@ -321,16 +328,6 @@ export class Optimizer {
process.stdout.write(statusline);

globals.convergence.push(ratioString);
}

// Increase Number of evaluations taken.
numEvals++;

if (numEvals >= this.args.evals) {
// DONE WITH OPTIMISING WRITE EVERYTHING TO DISK AND EXIT.
globals.time.generateCryptopt =
(Date.now() - optimistaionStartDate) / 1000 - globals.time.validate;
clearInterval(intervalHandle);

Logger.log("writing current asm");
const elapsed = Date.now() - optimistaionStartDate;
Expand All @@ -340,6 +337,7 @@ export class Optimizer {
paddedSeed,
ratioString,
evals: this.args.evals,
numEvals,
elapsed,
batchSize,
numBatches,
Expand All @@ -353,25 +351,28 @@ export class Optimizer {
});
Logger.log(statistics);

const [asmFile, mutationsCsvFile] = generateResultFilename(
{ ...this.args, symbolname: this.symbolname },
[`_ratio${ratioString.replace(".", "")}.asm`, `.csv`],
);

// write best found solution with headers
// flip, because we want the last accepted, not the last mutated.
const flipped = toggleFUNCTIONS(currentNameOfTheFunctionThatHasTheMutation);
if (writeout) {
// write best found solution with headers
// flip, because we want the last accepted, not the last mutated.
const flipped = toggleFUNCTIONS(currentNameOfTheFunctionThatHasTheMutation);

writeString(
asmFile,
["SECTION .text", `\tGLOBAL ${this.symbolname}`, `${this.symbolname}:`]
.concat(this.asmStrings[flipped])
.concat(statistics)
.join("\n"),
);
writeString(
asmFile,
["SECTION .text", `\tGLOBAL ${this.symbolname}`, `${this.symbolname}:`]
.concat(this.asmStrings[flipped])
.concat(statistics)
.join("\n"),
);

// writing the CSV
writeString(mutationsCsvFile, globals.mutationLog.join("\n"));
// writing the CSV
writeString(mutationsCsvFile, globals.mutationLog.join("\n"));
}
}
if (numEvals >= this.args.evals) {
// DONE WITH OPTIMISING WRITE EVERYTHING TO DISK AND EXIT.
globals.time.generateCryptopt =
(Date.now() - optimistaionStartDate) / 1000 - globals.time.validate;
clearInterval(intervalHandle);

if (shouldProof(this.args)) {
// and proof correct
Expand All @@ -395,6 +396,7 @@ export class Optimizer {

resolve(0);
}
numEvals++;
}
}, 0);
});
Expand Down
9 changes: 5 additions & 4 deletions src/optimizer/optimizer.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ export function genStatusLine(a: {

return [
// general
`${a.writeout ? "\n" : "\r"}${a.symbolname}`,
`${a.logComment ?? "-"}`,
`${bl}${a.stacklength.toString().padStart(3)}${re}`,
`${cy}bs${a.batchSize.toString().padStart(5)}${re}`,
Expand Down Expand Up @@ -79,13 +78,15 @@ export function genStatusLine(a: {
`${cy}${Model.decisionStats}${re}`,
`${a.kept ? gn : rd}${SI(a.numEvals)}(${((100 * a.numEvals) / a.evals).toFixed(0).padStart(2)}%)` +
`${pu}${a.show_per_second}${re}`,
`${a.writeout ? "\n" : "\r"}${a.symbolname}`,
].join("|");
}

export function genStatistics(a: {
paddedSeed: string;
ratioString: string;
evals: number;
numEvals: number;
elapsed: number;
batchSize: number;
numBatches: number;
Expand All @@ -106,10 +107,10 @@ export function genStatistics(a: {
`; using counter; ${a.counter}`,
`; framePointer ${a.framePointer}`,
`; memoryConstraints ${a.memoryConstraints}`,
`; time needed: ${a.elapsed} ms on ${a.evals} evaluations.`,
`; time needed: ${a.elapsed} ms on ${a.numEvals} evaluations.`,
`; Time spent for assembling and measuring (initial batch_size=${a.batchSize}, initial num_batches=${a.numBatches}): ${a.acc} ms`,
`; number of used evaluations: ${a.evals}`,
`; Ratio (time for assembling + measure)/(total runtime for ${a.evals} evals): ${a.acc / a.elapsed}`,
`; number of used evaluations: ${a.numEvals}/${a.evals}`,
`; Ratio (time for assembling + measure)/(total runtime for ${a.numEvals} evals): ${a.acc / a.elapsed}`,
...["permutation", "decision"].map((key) => {
const r = ((a.numRevert[key] / a.numMut[key]) * 100).toFixed(3);
return `; number reverted ${key} / tried ${key}: ${a.numRevert[key]} / ${a.numMut[key]} =${r}%`;
Expand Down
1 change: 1 addition & 0 deletions src/types/optimizer.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export type OptimizerArgs = {
cyclegoal: number;
readState?: string; // filename
logComment: string;
logEvery: number; // log line and write asm file every this much per cent. set to 5 to write every 5 per cent.
proof: boolean;
verbose: boolean;
bridge: BRIDGES_T;
Expand Down
1 change: 1 addition & 0 deletions test/test-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export function getTestArgs(filename: string): OptimizerArgs {
seed: 11, //Date.now(),
cyclegoal: 100,
logComment: "",
logEvery: 5,
proof: false,
bridge: "fiat",
curve,
Expand Down

0 comments on commit f250ffd

Please sign in to comment.