-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtriadicmemoryCL.c
156 lines (106 loc) · 4.39 KB
/
triadicmemoryCL.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
triadicmemoryCL.c
Triadic Memory Command Line
Copyright (c) 2022-2024 Peter Overmann
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the “Software”), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
#include "triadicmemory.h"
static int VERSIONMAJOR = 2;
static int VERSIONMINOR = 0;
static void print_help(void)
{
printf("triadicmemory %d.%d\n\n", VERSIONMAJOR, VERSIONMINOR);
printf("Sparse distributed memory for storing triple associations {x,y,z} of sparse binary hypervectors.\n");
printf("A hypervector of dimension n is given by an ordered set of p integers with values from 1 to n which represent its \"1\" bits.\n");
printf("\n");
printf("Command line arguments:\n\n");
printf("triadicmemory n p (n is the vector dimension, p is the vector's target sparse population)\n\n");
printf("Usage examples:\n\n");
printf("Store {x,y,z}:\n");
printf("{37 195 355 371 471 603 747 914 943 963, 73 252 418 439 461 469 620 625 902 922, 60 91 94 128 249 517 703 906 962 980}\n\n");
printf("Recall x:\n");
printf("{_ , 73 252 418 439 461 469 620 625 902 922, 60 91 94 128 249 517 703 906 962 980}\n\n");
printf("Recall y:\n\n");
printf("{37 195 355 371 471 603 747 914 943 963, _ , 160 91 94 128 249 517 703 906 962 980}\n\n");
printf("Recall z:\n\n");
printf("{37 195 355 371 471 603 747 914 943 963, 73 252 418 439 461 469 620 625 902 922, _}\n\n");
printf("Generate a random vector:\n");
printf("random\n\n");
printf("Print this help text:\n");
printf("help\n\n");
printf("Show version number:\n");
printf("version\n\n");
printf("Terminate process:\n");
printf("quit\n\n");
}
static char* parse (char *buf, SDR *s)
{
buf = sdr_parse(buf, s);
if (*buf == QUERY && s->p == 0) { s->p = -1; buf++; while (isspace(*buf)) buf++;}
if (*buf == SEPARATOR) buf++;
return buf;
}
int main(int argc, char *argv[])
{
char *buf, inputline[10000];
if (argc != 3)
{
print_help();
exit(1);
}
int N, P; // SDR dimension and target sparse population, received from command line
sscanf( argv[1], "%d", &N);
sscanf( argv[2], "%d", &P);
TriadicMemory *T = triadicmemory_new(N, P);
SDR *x = sdr_new(N);
SDR *y = sdr_new(N);
SDR *z = sdr_new(N);
while ( fgets(inputline, sizeof(inputline), stdin) != NULL)
{
if (! strcmp(inputline, "quit\n"))
exit(0);
if (! strcmp(inputline, "help\n"))
print_help();
else if (! strcmp(inputline, "random\n"))
sdr_print(sdr_random(x, P));
else if ( strcmp(inputline, "version\n") == 0)
printf("triadicmemory %d.%d\n", VERSIONMAJOR, VERSIONMINOR);
else // parse input of the form { 1 2 3, 4 5 6, 7 8 9 }
{
buf = inputline;
if (*buf != '{')
{ printf("expecting '{', found %s\n ", inputline); exit(4); }
buf = parse(parse(parse(buf+1, x), y), z);
if( *buf != '}')
{ printf("expecting '}', found %s\n ", inputline); exit(4); }
if ( x->p >= 0 && y->p >= 0 && z->p >= 0) // write x, y, z
triadicmemory_write (T, x, y, z);
else if ( x->p >= 0 && y->p >= 0 && z->p == -1) // read z
sdr_print( triadicmemory_read_z (T, x, y, z));
else if ( x->p >= 0 && y->p == -1 && z->p >= 0) // read y
sdr_print( triadicmemory_read_y (T, x, y, z));
else if ( x->p == -1 && y->p >= 0 && z->p >= 0) // read x
sdr_print( triadicmemory_read_x (T, x, y, z));
else
{ printf("invalid input\n"); exit(3); }
}
}
return 0;
}