commit def25ea64b097d2a994eb33c91dc66248233afec
parent f6df8471931faeac7be76b8cdedc6daa48ab961d
Author: Sebastiano Tronto <sebastiano.tronto@gmail.com>
Date: Sat, 12 Mar 2022 19:13:54 +0100
Added freemem command
Diffstat:
9 files changed, 57 insertions(+), 24 deletions(-)
diff --git a/TODO.md b/TODO.md
@@ -4,10 +4,11 @@ This is a list of things that I would like to add or change at some point.
It's more of a personal reminder than anything else.
## For version 2.1
-### Memory management
-* freemem command: implement the 5 functions, execute at the end of shell.c
### Installation
-* Is it possible to make table generation at least 3x faster?
+* Implement coord->move to apply moves directly on coordinates
+ (can this be used to improve solving speed? Applying moves on
+ three coordinates is better than applying a move on a Cube and
+ then transforming it, but I still need to work with inverses...)
### Documentation
* Write an examples.md file
* More screenshots!
@@ -55,9 +56,6 @@ including e.g. solutions that were not shown because -c)
* free pruning table after solve is done? if I do this I need to deafault to a
small table for < 8 moves solutions or smth
* improve multi-threading when solving multiple scrambles
-* alternative: just add a command "free" to free up memory; it is not
- user friendly (who wants to manage memory manually?) but on the other hand
- it will only be used by the few who have less than 4(?) Gb of ram.
* nissy -M maxmem option for running with at most maxmem memory; if exceeded
when loading a pruning table, return failure (or make every solve command
use tiny tables instead?); if maxmem is very 600Mb or
diff --git a/doc/nissy.1 b/doc/nissy.1
@@ -53,6 +53,10 @@ all moves done on inverse.
.It Nm commands
List all available commands.
.
+.It Nm freemem
+Release some large tables from memory. You can use this command in case
+you want to keep nissy open without using too much RAM.
+.
.It Nm gen Op Fl t Ar N
Generate all tables used by nissy. Run this to complete your installation.
If
diff --git a/nissy b/nissy
Binary files differ.
diff --git a/src/commands.c b/src/commands.c
@@ -162,6 +162,7 @@ version_cmd = {
Command *commands[] = {
&commands_cmd,
+ &freemem_cmd,
&gen_cmd,
&help_cmd,
&invert_cmd,
@@ -508,8 +509,8 @@ gen_exec(CommandArgs *args)
init_symcoord();
fprintf(stderr, "Generating pruning tables...\n");
- for (i = 0; allpd[i] != NULL; i++)
- genptable(allpd[i], args->opts->nthreads);
+ for (i = 0; all_pd[i] != NULL; i++)
+ genptable(all_pd[i], args->opts->nthreads);
fprintf(stderr, "Done!\n");
}
@@ -547,13 +548,16 @@ commands_exec(CommandArgs *args)
static void
freemem_exec(CommandArgs *args)
{
-/* TODO: implement these functions
- free_allpd();
- free_allsd();
- free_invtables();
- free_ttables();
- free_mtables();
-*/
+ int i;
+
+ for (i = 0; all_pd[i] != NULL; i++)
+ free_pd(all_pd[i]);
+
+ for (i = 0; all_sd[i] != NULL; i++)
+ free_sd(all_sd[i]);
+
+ /* TODO: invtables are also large, but for now they are *
+ * statically allocated. Consider releasing those too. */
}
static void
diff --git a/src/pruning.c b/src/pruning.c
@@ -89,7 +89,7 @@ pd_nxopt31_HTM = {
.fbmod = BINOM8ON4,
};
-PruneData * allpd[] = {
+PruneData * all_pd[] = {
&pd_eofb_HTM,
&pd_coud_HTM,
&pd_cornershtr_HTM,
@@ -117,6 +117,15 @@ findchunk(PruneData *pd, int nchunks, uint64_t i)
}
void
+free_pd(PruneData *pd)
+{
+ if (pd->generated)
+ free(pd->ptable);
+
+ pd->generated = false;
+}
+
+void
genptable(PruneData *pd, int nthreads)
{
bool compact;
diff --git a/src/pruning.h b/src/pruning.h
@@ -14,8 +14,9 @@ extern PruneData pd_htr_drud;
extern PruneData pd_htrfin_htr;
extern PruneData pd_nxopt31_HTM;
-extern PruneData * allpd[];
+extern PruneData * all_pd[];
+void free_pd(PruneData *pd);
void genptable(PruneData *pd, int nthreads);
void print_ptable(PruneData *pd);
uint64_t ptablesize(PruneData *pd);
diff --git a/src/shell.c b/src/shell.c
@@ -11,10 +11,10 @@ checkfiles()
char fname[strlen(tabledir)+100];
int i;
- for (i = 0; allpd[i] != NULL; i++) {
+ for (i = 0; all_pd[i] != NULL; i++) {
strcpy(fname, tabledir);
- strcpy(fname, "/");
- strcpy(fname, allpd[i]->filename);
+ strcat(fname, "/");
+ strcat(fname, all_pd[i]->filename);
if ((f = fopen(fname, "rb")) == NULL)
return false;
else
@@ -140,14 +140,16 @@ launch(bool batchmode)
int
main(int argc, char *argv[])
{
+ char *closing_cmd[1] = { "freemem" };
+
init_env();
if (!checkfiles()) {
fprintf(stderr,
"--- Warning ---\n"
- "Some pruning tables are missing or unreadable."
+ "Some pruning tables are missing or unreadable\n"
"You can generate them with `nissy gen -t 4'\n"
- "Here `4' is the number of threads. Use more if your"
+ "Here `4' is the number of threads. Use more if your "
"CPU has them, or expect it to take a while.\n"
"---------------\n\n"
);
@@ -163,7 +165,7 @@ main(int argc, char *argv[])
launch(false);
}
- /* TODO: exec freemem */
+ exec_args(1, closing_cmd);
return 0;
}
diff --git a/src/symcoord.c b/src/symcoord.c
@@ -53,7 +53,7 @@ sd_eofbepos_16 = {
.trans = trans_group_udfix
};
-static SymData * all_sd[] = {
+SymData * all_sd[] = {
&sd_cp_16,
&sd_eofbepos_16,
NULL
@@ -266,6 +266,18 @@ transfinder_nxopt31(uint64_t ind, Trans *ret)
/* Other functions ***********************************************************/
+void
+free_sd(SymData *sd)
+{
+ if (sd->generated) {
+ free(sd->class);
+ free(sd->rep);
+ free(sd->transtorep);
+ }
+
+ sd->generated = false;
+}
+
static void
gensym(SymData *sd)
{
diff --git a/src/symcoord.h b/src/symcoord.h
@@ -9,6 +9,9 @@ extern Coordinate coord_drud_sym16;
extern Coordinate coord_drudfin_noE_sym16;
extern Coordinate coord_nxopt31;
+extern SymData *all_sd[];
+
+void free_sd(SymData *sd);
void init_symcoord();
#endif