source: icGREP/icgrep-devel/icgrep/array-test.cpp @ 5361

Last change on this file since 5361 was 5361, checked in by nmedfort, 3 years ago

Work on non-carry collapsing mode.

File size: 9.3 KB
Line 
1/*
2 *  Copyright (c) 2015 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 *  icgrep is a trademark of International Characters.
5 */
6
7#include <IR_Gen/idisa_builder.h>                  // for IDISA_Builder
8#include <IR_Gen/idisa_target.h>                   // for GetIDISA_Builder
9#include <kernels/mmap_kernel.h>                   // for MMapSourceKernel
10#include <kernels/s2p_kernel.h>                    // for S2PKernel
11#include <kernels/alignedprint.h>
12#include <kernels/streamset.h>                     // for SingleBlockBuffer
13#include <kernels/pipeline.h>
14#include <llvm/ExecutionEngine/ExecutionEngine.h>  // for ExecutionEngine
15#include <llvm/IR/Function.h>                      // for Function, Function...
16#include <llvm/IR/Module.h>                        // for Module
17#include <llvm/IR/Verifier.h>                      // for verifyModule
18#include <llvm/Support/CommandLine.h>              // for ParseCommandLineOp...
19#include <llvm/Support/raw_ostream.h>              // for errs
20#include <pablo/pablo_kernel.h>                    // for PabloKernel
21#include <pablo/pe_zeroes.h>
22#include <pablo/pe_ones.h>
23#include <toolchain.h>                             // for JIT_to_ExecutionEn...
24#include <pablo/builder.hpp>                       // for PabloBuilder
25#include <boost/filesystem.hpp>
26#include <boost/iostreams/device/mapped_file.hpp>
27#include "llvm/ADT/StringRef.h"                    // for StringRef
28#include "llvm/IR/CallingConv.h"                   // for ::C
29#include "llvm/IR/DerivedTypes.h"                  // for ArrayType
30#include "llvm/IR/LLVMContext.h"                   // for LLVMContext
31#include "llvm/IR/Value.h"                         // for Value
32#include "llvm/Support/Debug.h"                    // for dbgs
33#include <pablo/pablo_toolchain.h>
34#include <iostream>
35
36namespace llvm { class Type; }
37namespace pablo { class Integer; }
38namespace pablo { class Var; }
39
40using namespace pablo;
41using namespace kernel;
42using namespace parabix;
43using namespace llvm;
44
45static cl::list<std::string> inputFiles(cl::Positional, cl::desc("<input file ...>"), cl::OneOrMore);
46
47void generate(PabloKernel * kernel) {
48
49    PabloBuilder pb(kernel->getEntryBlock());
50
51    Var * input = kernel->getInputStreamVar("input");
52
53    PabloAST * basis[8];
54    for (int i = 0; i < 8; ++i) {
55        basis[i] = pb.createExtract(input, i);
56    }
57
58    PabloAST * temp1 = pb.createOr(basis[0], basis[1], "temp1");
59    PabloAST * temp2 = pb.createAnd(basis[2], pb.createNot(basis[3]), "temp2");
60    PabloAST * temp3 = pb.createAnd(temp2, pb.createNot(temp1), "temp3");
61    PabloAST * temp4 = pb.createAnd(basis[4], pb.createNot(basis[5]), "temp4");
62    PabloAST * temp5 = pb.createOr(basis[6], basis[7], "temp5");
63    PabloAST * temp6 = pb.createAnd(temp4, pb.createNot(temp5), "temp6");
64    PabloAST * lparen = pb.createAnd(temp3, temp6, "lparens");
65    PabloAST * temp7 = pb.createAnd(basis[7], pb.createNot(basis[6]), "temp7");
66    PabloAST * temp8 = pb.createAnd(temp4, temp7, "temp8");
67    PabloAST * rparen = pb.createAnd(temp3, temp8, "rparens");
68    PabloAST * parens = pb.createOr(lparen, rparen, "parens");
69
70
71    Var * const pending_lparen = pb.createVar("pending_lparen", lparen);
72    Var * const all_closed = pb.createVar("all_closed", pb.createZeroes());
73    Var * const accumulated_errors = pb.createVar("accumulated_errors", pb.createZeroes());
74    Var * const in_play = pb.createVar("in_play", parens);
75    Var * const index = pb.createVar("i", pb.getInteger(0));
76
77    Var * matches = kernel->getOutputStreamVar("matches");
78
79    PabloBuilder body = PabloBuilder::Create(pb);
80
81    pb.createWhile(pending_lparen, body);
82
83        PabloAST * pscan = body.createAdvanceThenScanTo(pending_lparen, in_play, "pscan");
84
85        PabloAST * closed = body.createAnd(pscan, rparen, "closed_rparen");
86        body.createAssign(all_closed, body.createOr(all_closed, closed));
87
88        body.createAssign(pending_lparen, body.createAnd(pscan, lparen));
89        // Mark any opening paren without a matching closer as an error.
90        PabloAST * unmatched_lparen = body.createAtEOF(pscan, "unmatched_lparen");
91        body.createAssign(accumulated_errors, body.createOr(accumulated_errors, unmatched_lparen));
92
93        body.createAssign(body.createExtract(matches, index), closed);
94
95        PabloAST * pending_rparen = body.createAnd(rparen, body.createNot(all_closed, "open_rparen"), "pending_rparen");
96        body.createAssign(in_play, body.createOr(pending_lparen, pending_rparen));
97        body.createAssign(index, body.createAdd(index, body.getInteger(1)));
98
99    // Mark any closing paren that was not actually used to close an opener as an error.
100    PabloAST * const unmatched_rparen = pb.createAnd(rparen, pb.createNot(all_closed), "unmatched_rparen");
101    pb.createAssign(kernel->getOutputStreamVar("errors"), pb.createOr(accumulated_errors, unmatched_rparen));
102
103}
104
105Function * pipeline(IDISA::IDISA_Builder * iBuilder, const unsigned count) {
106
107    Type * byteStreamTy = iBuilder->getStreamSetTy(1, 8);
108
109    Module * const mod = iBuilder->getModule();
110
111    Function * const main = cast<Function>(mod->getOrInsertFunction("Main", iBuilder->getVoidTy(), byteStreamTy->getPointerTo(), iBuilder->getSizeTy(), nullptr));
112    main->setCallingConv(CallingConv::C);
113    Function::arg_iterator args = main->arg_begin();
114   
115    Value * const inputStream = &*(args++);
116    inputStream->setName("input");
117    Value * const fileSize = &*(args++);
118    fileSize->setName("fileSize");
119
120    const unsigned segmentSize = codegen::SegmentSize;
121    const unsigned bufferSegments = codegen::BufferSegments;
122   
123    ExternalFileBuffer ByteStream(iBuilder, iBuilder->getStreamSetTy(1, 8));
124
125    MMapSourceKernel mmapK(iBuilder, segmentSize);
126    mmapK.generateKernel({}, {&ByteStream});
127    mmapK.setInitialArguments({fileSize});
128
129    CircularBuffer BasisBits(iBuilder, iBuilder->getStreamSetTy(8), segmentSize * bufferSegments);
130
131    S2PKernel  s2pk(iBuilder);
132    s2pk.generateKernel({&ByteStream}, {&BasisBits});
133
134    PabloKernel bm(iBuilder, "MatchParens",
135        {Binding{iBuilder->getStreamSetTy(8), "input"}},
136        {Binding{iBuilder->getStreamSetTy(count), "matches"}, Binding{iBuilder->getStreamTy(), "errors"}});
137
138    generate(&bm);
139    pablo_function_passes(&bm);
140
141    ExpandableBuffer matches(iBuilder, iBuilder->getStreamSetTy(count), segmentSize * bufferSegments);
142    SingleBlockBuffer errors(iBuilder, iBuilder->getStreamTy());
143
144    bm.generateKernel({&BasisBits}, {&matches, &errors});
145
146    PrintStreamSet printer(iBuilder, {"matches", "errors"});
147    printer.generateKernel({&matches, &errors}, {});
148
149    iBuilder->SetInsertPoint(BasicBlock::Create(mod->getContext(), "entry", main, 0));
150
151    ByteStream.setStreamSetBuffer(inputStream, fileSize);
152    BasisBits.allocateBuffer();
153    matches.allocateBuffer();
154    errors.allocateBuffer();
155
156    generatePipelineLoop(iBuilder, {&mmapK, &s2pk, &bm, &printer});
157    iBuilder->CreateRetVoid();
158
159    return main;
160}
161
162typedef void (*MatchParens)(char * byteStream, size_t fileSize);
163
164MatchParens generateAlgorithm() {
165    LLVMContext ctx;
166    Module * M = new Module("mp", ctx);
167    IDISA::IDISA_Builder * idb = IDISA::GetIDISA_Builder(M);
168
169    llvm::Function * f = pipeline(idb, 3);
170
171    verifyModule(*M, &dbgs());
172
173    ExecutionEngine * wcEngine = JIT_to_ExecutionEngine(M);
174
175    wcEngine->finalizeObject();
176
177    delete idb;
178
179    return reinterpret_cast<MatchParens>(wcEngine->getPointerToFunction(f));
180}
181
182template <typename T>
183std::ostream & operator<< (std::ostream& out, const std::vector<T>& v) {
184  if ( !v.empty() ) {
185    out << '[';
186    std::copy (v.begin(), v.end(), std::ostream_iterator<T>(out, ", "));
187    out << "\b\b]";
188  }
189  return out;
190}
191
192void check(const char * byteStream, const size_t fileSize) {
193
194    std::vector<size_t> unmatched_left;
195    std::vector<size_t> unmatched_right;
196    size_t maxDepth = 0;
197    for (size_t i = 0; i < fileSize; ++i) {
198        switch (byteStream[i]) {
199            case '(':
200                unmatched_left.push_back(i);
201                maxDepth = std::max<size_t>(maxDepth, unmatched_left.size());
202                break;
203            case ')':
204                if (unmatched_left.empty()) {
205                    unmatched_right.push_back(i);
206                } else {
207                    unmatched_left.pop_back();
208                }
209            default:
210                break;
211        }
212    }
213
214    std::cerr << "maximum depth:        " << maxDepth << "\n"
215                 "invalid left parens:  " << unmatched_left << "\n"
216                 "invalid right parens: " << unmatched_right <<
217                 std::endl;
218}
219
220void run(MatchParens match, const std::string & fileName) {
221    const boost::filesystem::path file(fileName);
222    if (exists(file)) {
223        if (is_directory(file)) {
224            return;
225        }
226        size_t fileSize = file_size(file);
227        boost::iostreams::mapped_file_source mappedFile;
228        if (fileSize > 0) {
229            mappedFile.open(fileName);
230            char * fileBuffer = const_cast<char *>(mappedFile.data());
231            check(fileBuffer, fileSize);
232            match(fileBuffer, fileSize);
233            mappedFile.close();
234        }
235    } else {
236        std::cerr << "Error: cannot open " << fileName << " for processing. Skipped.\n";
237    }
238}
239
240int main(int argc, char *argv[]) {
241    cl::ParseCommandLineOptions(argc, argv);
242    auto f = generateAlgorithm();
243    for (const auto & inputFile : inputFiles) {
244        run(f, inputFile);
245    }
246    return 0;
247}
Note: See TracBrowser for help on using the repository browser.