-
Notifications
You must be signed in to change notification settings - Fork 0
/
neuron.cpp
85 lines (73 loc) · 1.7 KB
/
neuron.cpp
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
#include <tbb/queuing_mutex.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <cstring>
#include <chrono>
#include <iostream>
namespace neuron
{
// this is meant to prevent multiple concurrent calls into HDF5
// and it is also used to serialize debug output when MEM_CHECK
// is defined
tbb::queuing_mutex ioMutex;
// --------------------------------------------------------------------------
int neuronFileExists(const char *dirName, int nid)
{
char fn[512];
snprintf(fn, 511, "%s/seg_coords/%d.h5", dirName, nid);
int fd = open(fn, O_RDONLY);
if (fd == -1)
{
return 0;
}
close(fd);
return 1;
}
// --------------------------------------------------------------------------
int imFileExists(const char *dirName)
{
char fn[512];
snprintf(fn, 511, "%s/im.h5", dirName);
int fd = open(fn, O_RDONLY);
if (fd == -1)
{
return 0;
}
close(fd);
return 1;
}
// --------------------------------------------------------------------------
int scanForNeurons(const char *dirName, int &lastId)
{
// scan for an id that fails. this locates the next power of 2 above the
// the last valid file
int bot = 1;
int top = 1;
while (neuronFileExists(dirName, top - 1))
{
bot = top;
top *= 2;
}
// use bisection to locate the last valid file.
--bot;
--top;
while ((top - bot) > 1)
{
int next = bot + (top - bot) / 2;
if (neuronFileExists(dirName, next))
{
bot = next;
}
else
{
top = next;
}
}
// verify
if (!neuronFileExists(dirName, bot))
return -1;
lastId = bot;
return 0;
}
}