# One simple problem, (too) many solutions with MitrionC

In preparation to tackle a bigger problem, I decided to code a MitrionC program that does the following:

1. reads MEMSIZE numbers from an external memory and writes them to an internal memory (I call these numbers the ‘database’)
2. reads 1 number from another external memory (I call this number the ‘query’)
3. determine if the query is equal to any of the database numbers.

#### Attempt #1

```Mitrion-C 1.5;

MEMSIZE = 100;
MemElem = typedef int:8;

AttRAM = typedef mem MemElem[MEMSIZE];

(bool ,AttRAM ) main(AttRAM RAM0_it0, AttRAM RAM1_it0)
{

LUT_it0 = _memcreate(mem MemElem[MEMSIZE] LUT_it_last);
LUT_it_last = LUT_it0;
AttRAM  LUT_it3 = LUT_it0;

(MemElem list, LUT_it2) =
foreach (i in <0 .. MEMSIZE-1>)
{
LUT_it1 = memwrite(LUT_it0,d,i);
} (d,LUT_it1);

bool e = false;
(found,LUT_it4)= for (i in <0 .. MEMSIZE-1>) {
//found = for (element in list) {
e = if ( element == query) true else e;
} (e,LUT_it3) ;

} (found, LUT_it4);

```

Observations:
Too sequential!!

Results:

 MEMSIZE CYCLES 10 79 20 142 30 204 40 267 100 642

#### Attempt #2

```Mitrion-C 1.5;

/*
Given a list of size LISTSIZE determine if it contains duplicates.
Version 2.
*/

LISTSIZE = 10;
ElemType = typedef int:8;

(bool) main(ElemType  list)
{
vector = reformat(list, [LISTSIZE]);
comp_list_of_lists = foreach (e,i in list  ,<0 .. LISTSIZE-1>) {
comp_list = foreach(f,j in vector,[0 .. LISTSIZE-1]) {
comp = ( (i!=j) && (f == e));
} comp;
} comp_list;

gathered_comp_list = foreach (e  in comp_list_of_lists) {
temp_vector = reformat(e, [LISTSIZE]);
bits:LISTSIZE scalar = temp_vector;
f = if ((uint:LISTSIZE)scalar != 0) true else false;
} f;

regathered_comp_list = reformat(gathered_comp_list, [LISTSIZE]);
bits:LISTSIZE scalar = regathered_comp_list;
bool found = if ((uint:LISTSIZE)scalar  != 0) true else false;
watch found;
} found;
```

Observations:
Creates MEMSIZE structures in parallel. For example, here’s the resulting structure for MEMSIZE=10.

Results:

 MEMSIZE CYCLES 10 60 20 80 30 100 100 240

#### Attempt #3

```Mitrion-C 1.5;

MEMSIZE = 10;
SEARCHSIZE = 10;
MemElem = typedef int:8;
bits:SEARCHSIZE ZERO = 0x000;

AttRAM = typedef mem MemElem[MEMSIZE];

(bool) main(AttRAM RAM0_it0, AttRAM RAM1_it0)
{
// This is the internal memory
LUT_it0 = _memcreate(mem MemElem[MEMSIZE] LUT_it_last);
LUT_it_last = LUT_it0;
AttRAM  LUT_it3 = LUT_it0;

// Read from external memory and write to internal LUT

(MemElem list, LUT_it2) =
foreach (i in <0 .. MEMSIZE-1>)
{
LUT_it1 = memwrite(LUT_it0,d,i);
} (d,LUT_it1);

// Compare query against each element in the DBase
(found_list,LUT_it04)  = foreach (i in <0 .. SEARCHSIZE-1>) {
e = if ( element == query) 1 else 0;
}(e, LUT_it3);

bool e = false;
found_vector = reformat(found_list, [SEARCHSIZE]);
watch found_vector;
bits:SEARCHSIZE scalar = found_vector;

bool found = if ((uint:SEARCHSIZE)scalar > 0) true else false;
// recred(found_vector);
watch found;

} (found);
```

Observations:
????

Results:

 MEMSIZE CYCLES 10 57 20 77 30 99 100 257

#### Attempt #4

```Mitrion-C 1.5;

MEMSIZE = 100;
SEARCHSIZE = 100;
MemElem = typedef int:8;
bits:SEARCHSIZE ZERO = 0x000;

AttRAM = typedef mem MemElem[MEMSIZE];

(bool) main(AttRAM RAM0_it0, AttRAM RAM1_it0)
{
// This is the internal memory
LUT_it0 = _memcreate(mem MemElem[MEMSIZE] LUT_it_last);
LUT_it_last = LUT_it0;
AttRAM  LUT_it3 = LUT_it0;

// Read from external memory and write to internal LUT
(MemElem list, LUT_it2) =
foreach (i in <0 .. MEMSIZE-1>)
{
LUT_it1 = memwrite(LUT_it0,d,i);
} (d,LUT_it1);

int:8 i = 0;
bool e = false;
// Compare query against each element in the DBase
(found,LUT_it04)  = while ( (e == false) && ( i < SEARCHSIZE) ) {
//found = for (element in list) {
e = if ( element == query) true else false;
i = i + 1;
}(e, LUT_it3);

} (found);
```

Observations:
The use of while serializes everything. Avoid!

Results:

 MEMSIZE CYCLES 10 164 20 317 30 469 100 1537
This entry was posted in MitrionC. Bookmark the permalink.