|
| 1 | +// RUN: %clang++ -std=c++11 -fno-exceptions -ffast-math -O0 %s -S -emit-llvm -o - | %opt - %loadEnzyme -enzyme -S |
| 2 | +// RUN: %clang++ -std=c++11 -fno-exceptions -ffast-math -O1 %s -S -emit-llvm -o - | %opt - %loadEnzyme -enzyme -S |
| 3 | +// RUN: %clang++ -std=c++11 -fno-exceptions -ffast-math -O2 %s -S -emit-llvm -o - | %opt - %loadEnzyme -enzyme -S |
| 4 | +// RUN: %clang++ -std=c++11 -fno-exceptions -ffast-math -O3 %s -S -emit-llvm -o - | %opt - %loadEnzyme -enzyme -S |
| 5 | +// RUN: %clang++ -std=c++11 -fno-exceptions -ffast-math -O0 %s -S -emit-llvm -o - | %opt - %loadEnzyme -enzyme -enzyme-inline=1 -S |
| 6 | +// RUN: %clang++ -std=c++11 -fno-exceptions -ffast-math -O1 %s -S -emit-llvm -o - | %opt - %loadEnzyme -enzyme -enzyme-inline=1 -S |
| 7 | +// RUN: %clang++ -std=c++11 -fno-exceptions -ffast-math -O2 %s -S -emit-llvm -o - | %opt - %loadEnzyme -enzyme -enzyme-inline=1 -S |
| 8 | +// RUN: %clang++ -std=c++11 -fno-exceptions -ffast-math -O3 %s -S -emit-llvm -o - | %opt - %loadEnzyme -enzyme -enzyme-inline=1 -S |
| 9 | + |
| 10 | +#include "test_utils.h" |
| 11 | +#include <fstream> |
| 12 | +#include <iostream> |
| 13 | +#include <sstream> |
| 14 | +#include <utility> |
| 15 | + |
| 16 | +extern double __enzyme_autodiff(void *, double); |
| 17 | + |
| 18 | +double fn(double vec) { |
| 19 | + std::string s("many more tests"); |
| 20 | + std::ifstream ifs(s.c_str(), std::ios_base::binary | std::ios_base::in); |
| 21 | + std::ifstream ifs1(s.c_str(), std::ios_base::binary | std::ios_base::out); |
| 22 | + std::ifstream ifs2(s.c_str(), std::ios_base::binary | std::ios_base::app); |
| 23 | + std::ifstream ifs3(s.c_str(), std::ios_base::binary | std::ios_base::trunc); |
| 24 | + std::ifstream ifs4(s.c_str(), std::ios_base::binary | std::ios_base::binary); |
| 25 | + std::ifstream ifs5(s.c_str(), std::ios_base::binary | std::ios_base::ate); |
| 26 | + if (ifs) { |
| 27 | + char buffer[20]; |
| 28 | + if (!ifs.read(buffer, 20)) { |
| 29 | + // Handle error |
| 30 | + } |
| 31 | + std::ifstream &ignore(int n = 1, int delim = EOF); |
| 32 | + } |
| 33 | + std::filebuf fb; |
| 34 | + fb.open("%S/Inputs/input.txt", std::ios::out); |
| 35 | + std::ostream os(&fb); |
| 36 | + os << "Test sentence\n"; |
| 37 | + vec *= 2; |
| 38 | + fb.close(); |
| 39 | + |
| 40 | + std::ifstream is; |
| 41 | + std::filebuf *fb2 = is.rdbuf(); |
| 42 | + |
| 43 | + // construct output string stream (buffer) - need <sstream> header |
| 44 | + std::ostringstream sout; |
| 45 | + |
| 46 | + // Write into string buffer |
| 47 | + sout << "apple" << std::endl; |
| 48 | + sout << "orange" << std::endl; |
| 49 | + sout << "banana" << std::endl; |
| 50 | + |
| 51 | + // Get contents |
| 52 | + std::cout << sout.str() << std::endl; |
| 53 | + |
| 54 | + std::string filename = "test.bin"; |
| 55 | + |
| 56 | + // Write to File |
| 57 | + std::ofstream fout(filename.c_str(), std::ios::out | std::ios::binary); |
| 58 | + if (!fout.is_open()) { |
| 59 | + std::cerr << "error: open file for output failed!" << std::endl; |
| 60 | + abort(); |
| 61 | + } |
| 62 | + int i = 1234; |
| 63 | + double d = 12.34; |
| 64 | + fout.write((char *)&i, sizeof(int)); |
| 65 | + fout.write((char *)&d, sizeof(double)); |
| 66 | + fout.close(); |
| 67 | + |
| 68 | + // Read from file |
| 69 | + std::ifstream fin(filename.c_str(), std::ios::in | std::ios::binary); |
| 70 | + if (!fin.is_open()) { |
| 71 | + std::cerr << "error: open file for input failed!" << std::endl; |
| 72 | + abort(); |
| 73 | + } |
| 74 | + int i_in; |
| 75 | + double d_in; |
| 76 | + fin.read((char *)&i_in, sizeof(int)); |
| 77 | + std::cout << i_in << std::endl; |
| 78 | + fin.read((char *)&d_in, sizeof(double)); |
| 79 | + std::cout << d_in << std::endl; |
| 80 | + fin.close(); |
| 81 | + |
| 82 | + char mybuffer[512]; |
| 83 | + std::filebuf *optr = new std::filebuf(); |
| 84 | + optr->pubsetbuf(mybuffer, 512); |
| 85 | + const char sentence[] = "Sample sentence"; |
| 86 | + auto ptr = |
| 87 | + optr->open("bd.bin", std::ios::binary | std::ios::trunc | std::ios::out); |
| 88 | + if (ptr) { |
| 89 | + float fx = 13; |
| 90 | + auto n = optr->sputn(sentence, sizeof(sentence) - 1); |
| 91 | + n += optr->sputn(reinterpret_cast<const char *>(&fx), sizeof(fx)); |
| 92 | + optr->pubsync(); |
| 93 | + } |
| 94 | + optr->close(); |
| 95 | + if (optr) { |
| 96 | + delete optr; |
| 97 | + } |
| 98 | + |
| 99 | + return vec * vec; |
| 100 | +} |
| 101 | + |
| 102 | +int main() { |
| 103 | + double x = 2.1; |
| 104 | + double dsq = __enzyme_autodiff((void*)fn, x); |
| 105 | + |
| 106 | + APPROX_EQ(dsq, 2.1 * 2 * 2 * x, 1e-7); |
| 107 | +} |
0 commit comments