def test_MakeDriver_CompileDriver_hello_world(): """And end-to-end test.""" testcase = deepsmith_pb2.Testcase( inputs={ "lsize": "1,1,1", "gsize": "1,1,1", "src": "kernel void A(global int* a) {a[get_global_id(0)] += 10;}", }) driver = cldrive.MakeDriver(testcase, True) with tempfile.TemporaryDirectory() as d: binary = cldrive.CompileDriver(driver, pathlib.Path(d) / "exe", 0, 0, timeout_seconds=60) proc = oclgrind.Exec([str(binary)]) assert "[cldrive] Platform:" in proc.stderr assert "[cldrive] Device:" in proc.stderr assert "[cldrive] OpenCL optimizations: on\n" in proc.stderr assert '[cldrive] Kernel: "A"\n' in proc.stderr assert "done.\n" in proc.stderr assert proc.stdout.split("\n")[-2] == ( "global int * a: 10 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 86 87 88 89 90 91 92 93 " "94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 " "114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 " "132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 " "150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 " "168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 " "186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 " "204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 " "222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 " "240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255")
def test_CompileDriver_DriverCompilationError_syntax_error(): """Test that DriverCompilationError is raised if code does not compile.""" with tempfile.TemporaryDirectory() as d: with pytest.raises(cldrive.DriverCompilationError): cldrive.CompileDriver("ina39lid s#yntax!", pathlib.Path(d) / 'exe', 0, 0, timeout_seconds=60) assert not (pathlib.Path(d) / 'exe').is_file()
def test_CompileDriver_returned_path(): """Test that output path is returned.""" with tempfile.TemporaryDirectory() as d: p = cldrive.CompileDriver("int main() {}", pathlib.Path(d) / "exe", 0, 0, timeout_seconds=60) assert p == pathlib.Path(d) / "exe"
def test_CompileDriver_null_c(): """Test compile a C program which does nothing.""" with tempfile.TemporaryDirectory() as d: p = cldrive.CompileDriver("int main() {return 0;}", pathlib.Path(d) / 'exe', 0, 0, timeout_seconds=60) assert p.is_file()
def test_CompileDriver_valid_cflags(): """Test that additional cflags are passed to build.""" with tempfile.TemporaryDirectory() as d: cldrive.CompileDriver('MY_TYPE main() {}', pathlib.Path(d) / 'exe', 0, 0, cflags=['-DMY_TYPE=int']) assert (pathlib.Path(d) / 'exe').is_file()
def test_CompileDriver_invalid_cflags(): """Test that DriverCompilationError is raised if cflags are invalid.""" with tempfile.TemporaryDirectory() as d: with pytest.raises(cldrive.DriverCompilationError): cldrive.CompileDriver('int main() {}', pathlib.Path(d) / 'exe', 0, 0, cflags=['--not_a_real_flag'])
def test_CompileDriver_invalid_cflags(): """Test that DriverCompilationError is raised if cflags are invalid.""" with tempfile.TemporaryDirectory() as d: with test.Raises(cldrive.DriverCompilationError): cldrive.CompileDriver( "int main() {}", pathlib.Path(d) / "exe", 0, 0, cflags=["--not_a_real_flag"], )
def test_CompileDriver_opencl_header(): """Test compile a C program which includes the OpenCL headers.""" with tempfile.TemporaryDirectory() as d: p = cldrive.CompileDriver(""" #ifdef __APPLE__ #include <OpenCL/opencl.h> #else #include <CL/cl.h> #endif int main() {} """, pathlib.Path(d) / 'exe', 0, 0, timeout_seconds=60) assert p.is_file()
def test_CompileDriver_hello_world_c(): """Test compile a C program which prints "Hello, world!".""" with tempfile.TemporaryDirectory() as d: p = cldrive.CompileDriver(""" #include <stdio.h> int main() { printf("Hello, world!\\n"); return 0; } """, pathlib.Path(d) / 'exe', 0, 0, timeout_seconds=60) assert p.is_file() output = subprocess.check_output([p], universal_newlines=True) assert output == "Hello, world!\n"
def test_GenerateDeadcodeMutations_fuzz_test_batch(i: int): """Fuzz test the mutation generator. For each round of testing: Generate a batch of mutated kernels. For each mutated kernel: Create a DeepSmith testcase to compile and run the kernel. Drive the testcase using oclgrind. Ensure that the testcase completes. """ del i # unused # Select random parameters for test. kernels = [random.choice(KERNELS)] + [ k for k in KERNELS if random.random() < 0.5 ] seed = random.randint(0, 1e9) num_permutations_of_kernel = random.randint(1, 5) num_mutations_per_kernel_min = random.randint(1, 5) num_mutations_per_kernel_max = num_mutations_per_kernel_min + random.randint( 1, 5 ) num_mutations_per_kernel = ( num_mutations_per_kernel_min, num_mutations_per_kernel_max, ) app.Log( 1, "num_kernels=%d, seed=%d, num_permutations_of_kernel=%d, " "num_mutations_per_kernel=%s", len(kernels), seed, num_permutations_of_kernel, num_mutations_per_kernel, ) # Generate a batch of mutations. generator = dci.GenerateDeadcodeMutations( kernels=kernels, rand=np.random.RandomState(seed), num_permutations_of_kernel=num_permutations_of_kernel, num_mutations_per_kernel=num_mutations_per_kernel, ) for i, mutated_kernel in enumerate(generator): app.Log(1, "Testing mutated kernel: %s", mutated_kernel) # Create a DeepSmith testcase for the mutated kernel. testcase = deepsmith_pb2.Testcase( inputs={"lsize": "1,1,1", "gsize": "1,1,1", "src": mutated_kernel,} ) # Make a driver for the testcase. driver = cldrive.MakeDriver(testcase, optimizations=True) with tempfile.TemporaryDirectory(prefix="phd_") as d: # Compile the driver. binary = cldrive.CompileDriver( driver, pathlib.Path(d) / "exe", 0, 0, timeout_seconds=60 ) # Execute the driver. proc = oclgrind.Exec([str(binary)]) app.Log(1, "Testcase driver output: '%s'", proc.stderr.rstrip()) assert not proc.returncode assert "[cldrive] Platform:" in proc.stderr assert "[cldrive] Device:" in proc.stderr assert "[cldrive] OpenCL optimizations: on\n" in proc.stderr assert '[cldrive] Kernel: "' in proc.stderr assert "done.\n" in proc.stderr # Sanity check that the correct number of kernels have been generated. app.Log(1, "Generated %d mutations", i + 1) assert i + 1 == len(kernels) * num_permutations_of_kernel