File: SharedSuffixArray.hpp

package info (click to toggle)
pbseqlib 5.3.5%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 7,020 kB
  • sloc: cpp: 77,250; python: 331; sh: 103; makefile: 41
file content (84 lines) | stat: -rw-r--r-- 2,791 bytes parent folder | download | duplicates (4)
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
#ifndef _BLASR_SHARED_SUFFIX_ARRAY_HPP_
#define _BLASR_SHARED_SUFFIX_ARRAY_HPP_

#include <cstdlib>
#include <fstream>
#include <sstream>

#include <alignment/algorithms/compare/CompareStrings.hpp>
#include <alignment/ipc/SharedMemoryAllocator.hpp>
#include <alignment/suffixarray/SuffixArray.hpp>
#include <alignment/tuples/CompressedDNATuple.hpp>
#include <alignment/tuples/DNATuple.hpp>

template <typename T, typename Sigma, typename Compare = DefaultCompareStrings<T>,
          typename Tuple = DNATuple>
class SharedSuffixArray : public SuffixArray<T, Sigma, Compare, Tuple>
{
public:
    std::string shmIdTag;
    SAIndex *indexShared;
    int indexID;
    std::string indexHandle;
    SAIndex *lookupTableShared;
    int lookupTableID;
    std::string lookupTableHandle;
    int lookupPrefixLength;

    void InitShmIdTag()
    {
        std::stringstream tagStrm;
        tagStrm << "_" << getpid();
        shmIdTag = tagStrm.str();
    }

    void ReadSharedArray(std::ifstream &saIn)
    {
        std::cout << "reading a shared suffix array index." << std::endl;
        saIn.read((char *)&this->length, sizeof(int));
        indexHandle = "suffixarray.index." + shmIdTag;
        AllocateMappedShare(indexHandle, this->length + 1, indexShared, indexID);
        std::cout << "the shared index is: " << indexShared << std::endl;
        this->index = indexShared;
        std::cout << "the index used is: " << this->index << std::endl;
        this->ReadAllocatedArray(saIn);
    }

    void ReadSharedLookupTable(std::ifstream &saIn)
    {
        this->ReadLookupTableLengths(saIn);
        lookupTableHandle = "suffixarray.lookuptable." + shmIdTag;
        AllocateMappedShare(lookupTableHandle, this->lookupTableLength + 1, lookupTableShared,
                            lookupTableID);
        this->lookupTable = lookupTableShared;
        this->ReadAllocatedLookupTable(saIn);
    }

    void ReadShared(std::string &inFileName)
    {
        std::ifstream saIn;
        InitShmIdTag();
        saIn.open(inFileName.c_str(), std::ios::binary);
        this->ReadComponentList(saIn);
        if (this->componentList[SuffixArray<T, Sigma, Compare, Tuple>::CompArray]) {
            this->ReadSharedArray(saIn);
        }
        if (this->componentList[SuffixArray<T, Sigma, Compare, Tuple>::CompLookupTable]) {
            this->ReadSharedLookupTable(saIn);
        }
        saIn.close();
    }

    void FreeShared()
    {

        if (this->componentList[SuffixArray<T, Sigma, Compare, Tuple>::CompArray]) {
            shm_unlink(indexHandle.c_str());
        }
        if (this->componentList[SuffixArray<T, Sigma, Compare, Tuple>::CompLookupTable]) {
            shm_unlink(lookupTableHandle.c_str());
        }
    }
};

#endif  // _BLASR_SHARED_SUFFIX_ARRAY_HPP_