Repository navigation
Expand file tree
/
Copy pathMemory FFI.m
More file actions
137 lines (99 loc) · 5.31 KB
/
Copy pathMemory FFI.m
File metadata and controls
137 lines (99 loc) · 5.31 KB
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
(* ::Package:: *)
(************************************************************************)\012(* This file was generated automatically by the Mathematica front end. *)\012(* It contains Initialization cells from a Notebook file, which *)\012(* typically will have the same name as this file except ending in *)\012(* ".nb" instead of ".m". *)\012(* *)\012(* This file is intended to be loaded into the Mathematica kernel using *)\012(* the package loading commands Get or Needs. Doing so is equivalent *)\012(* to using the Evaluate Initialization Cells menu command in the front *)\012(* end. *)\012(* *)\012(* DO NOT EDIT THIS FILE. This entire file is regenerated *)\012(* automatically each time the parent Notebook file is saved in the *)\012(* Mathematica front end. Any changes you make to this file will be *)\012(* overwritten. *)\012(************************************************************************)\012\012
(*
Copyright (c) 2026 Peter Overmann
SPDX-License-Identifier: MIT
This file is part of the "Creating Intelligence" project. It is licensed
under the MIT License. You may obtain a copy of the License in the LICENSE
file in the root directory of this repository.
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.
*)
(*
Uses the Standard C backend implementation of topological associative memory.
*)
Memory[config_Association] :=
Module[
{M, NA, PA, NB, PB, T, new, setthreshold, getthreshold, setresidual,
getresidual, getmatching, clearmatching, memory, read, write, store,
retrieve, matching, free, clear, memorycount},
new = ForeignFunctionLoad["TAM", "TAMnew",
{"CInt", "CInt", "CInt", "CInt"} -> "OpaqueRawPointer"];
setthreshold = ForeignFunctionLoad["TAM", "TAMsetthreshold",
{"OpaqueRawPointer", "CInt"} -> "Void"];
getthreshold = ForeignFunctionLoad["TAM", "TAMgetthreshold",
{"OpaqueRawPointer"} -> "CInt"];
setresidual = ForeignFunctionLoad["TAM", "TAMsetresidual",
{"OpaqueRawPointer", "CInt"} -> "Void"];
getresidual = ForeignFunctionLoad["TAM", "TAMgetresidual",
{"OpaqueRawPointer"} -> "CInt"];
getmatching = ForeignFunctionLoad["TAM", "TAMgetmatching",
{"OpaqueRawPointer", "RawPointer"::["CInt"],
"RawPointer"::["CInt"]} -> "Void"];
clearmatching = ForeignFunctionLoad["TAM", "TAMclearmatching",
{"OpaqueRawPointer"} -> "Void"];
memory = ForeignFunctionLoad["TAM", "TAMmemory",
{"OpaqueRawPointer"} -> "CInt"];
write = ForeignFunctionLoad["TAM", "TAMwrite",
{"OpaqueRawPointer", "RawPointer"::["CInt"], "CInt",
"RawPointer"::["CInt"], "CInt"} -> "Void"];
read = ForeignFunctionLoad["TAM", "TAMread",
{"OpaqueRawPointer", "RawPointer"::["CInt"], "CInt",
"RawPointer"::["CInt"], "RawPointer"::["CInt"]} -> "Void"];
free = ForeignFunctionLoad["TAM", "TAMfree",
{"OpaqueRawPointer"} -> "Void"];
{NA, PA} = config["A_parameters"];
{NB, PB} = config["B_parameters"];
If[ ! MatchQ[{NA, PA, NB, PB}, {__Integer}],
Message[Memory::params, config]];
M = new[NA, PA, NB, PB]; (* Memory constructor. *)
(* Push user-defined (scaled) threshold. *)
If[KeyExistsQ[config, "threshold"] && NumberQ[config["threshold"]],
setthreshold[M, Round[config["threshold"] * PA]]];
(* Push optional residual: the number of non-matching query elements
that may be absorbed as noise, in multiples of PA. *)
If[KeyExistsQ[config, "residual"] && NumberQ[config["residual"]]
&& config["residual"] >= 0,
setresidual[M, Floor[config["residual"] * PA + 10^-9]]];
(* Get final threshold. *)
T = getthreshold[M];
(* Store auto-association A -> A in memory. *)
store[A_List] := store[A, A];
(* Store hetero-association A -> B in memory. *)
store[A_List, B_List] :=
If[Length[A] > 0 && Length[B] > 0,
write[M, RawMemoryExport[A, "CInt"],
Length[A], RawMemoryExport[B, "CInt"], Length[B]]];
(* Retrieve. *)
retrieve[A_List] := Module[ {Y, Yn},
If[Length[A] === 0, clearmatching[M]; Return[{}]];
Y = RawMemoryAllocate["CInt", NB];
Yn = RawMemoryAllocate["CInt"];
read[M, RawMemoryExport[A, "CInt"], Length[A], Y, Yn];
RawMemoryImport[Y, {"List", RawMemoryRead[Yn]}]
];
(* Matching elements from the last retrieval. Empty upon
initialization and after a failed retrieval. *)
matching := Module[ {X, Xn},
X = RawMemoryAllocate["CInt", NA];
Xn = RawMemoryAllocate["CInt"];
getmatching[M, X, Xn];
RawMemoryImport[X, {"List", RawMemoryRead[Xn]}]
];
clear := free[M];
memorycount := memory[M];
<|
"A_parameters" -> {NA, PA},
"B_parameters" -> {NB, PB},
"T" -> T, (* Absolute pattern matching threshold. *)
"residual" -> getresidual[M],
"store" -> store,
"retrieve" -> retrieve,
"matching" :> matching,
"clear" :> clear,
"memorycount" :> memorycount,
"backend" -> "C_FFI"
|>
]
Memory::params = "Invalid hyperparameters: `1`";