Exemple #1
0
def UnpackResult(result_path: typing.Optional[str]) -> None:
  """Unpack a result proto into its components.

  Args:
    result_path: The path of the result to unpack.

  Raises:
    UsageError: In case of error.
  """
  result_to_unpack = ResultProtoFromFlag(result_path)
  unpack_dir = pathlib.Path(result_path).parent
  WriteFile(unpack_dir / "kernel.cl", result_to_unpack.testcase.inputs["src"])
  WriteFile(unpack_dir / "stdout.txt", result_to_unpack.outputs["stdout"])
  WriteFile(unpack_dir / "stderr.txt", result_to_unpack.outputs["stderr"])

  if result_to_unpack.testcase.harness.name == "cldrive":
    WriteFile(
      unpack_dir / "driver.c",
      cldrive.MakeDriver(result_to_unpack.testcase, FLAGS.opencl_opt),
    )
  elif result_to_unpack.testcase.harness.name == "cl_launcher":
    shutil.copyfile(CL_LAUNCHER_SRC, unpack_dir / "driver.c")
  else:
    raise app.UsageError(
      f"Unrecognized harness: '{result.testcase.harness.name}'"
    )
Exemple #2
0
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")
Exemple #3
0
def test_MakeDriver_ValueError_no_src():
    """Test that ValueError is raised if src input not set."""
    testcase = deepsmith_pb2.Testcase(inputs={
        "lsize": "1,1,1",
        "gsize": "1,1,1",
    })
    with test.Raises(ValueError) as e_ctx:
        cldrive.MakeDriver(testcase, True)
    assert "Field not set: 'Testcase.inputs[\"src\"]'" == str(e_ctx.value)
Exemple #4
0
def test_MakeDriver_ValueError_no_lsize():
    """Test that ValueError is raised if lsize input not set."""
    testcase = deepsmith_pb2.Testcase(inputs={
        "gsize": "1,1,1",
        "src": "kernel void A() {}"
    })
    with test.Raises(ValueError) as e_ctx:
        cldrive.MakeDriver(testcase, True)
    assert "Field not set: 'Testcase.inputs[\"lsize\"]'" == str(e_ctx.value)
Exemple #5
0
def test_MakeDriver_optimizations_on():
    """Test that OpenCL optimizations are enabled when requested."""
    testcase = deepsmith_pb2.Testcase(inputs={
        "lsize": "1,1,1",
        "gsize": "1,1,1",
        "src": "kernel void A() {}"
    })
    src = cldrive.MakeDriver(testcase, True)
    assert "[cldrive] OpenCL optimizations: on" in src
    assert "clBuildProgram(program, 0, NULL, NULL, NULL, NULL);" in src
Exemple #6
0
def test_MakeDriver_ValueError_invalid_gsize():
    """Test that ValueError is raised if gsize is not an NDRange."""
    testcase = deepsmith_pb2.Testcase(inputs={
        "lsize": "1,1,1",
        "gsize": "abc",
        "src": "kernel void A() {}"
    })
    with test.Raises(ValueError) as e_ctx:
        cldrive.MakeDriver(testcase, True)
    assert "invalid literal for int() with base 10: 'abc'" == str(e_ctx.value)
Exemple #7
0
def test_MakeDriver_optimizations_on():
  """Test that OpenCL optimizations are enabled when requested."""
  testcase = deepsmith_pb2.Testcase(inputs={
    'lsize': "1,1,1",
    'gsize': "1,1,1",
    'src': 'kernel void A() {}'
  })
  src = cldrive.MakeDriver(testcase, True)
  assert '[cldrive] OpenCL optimizations: on' in src
  assert 'clBuildProgram(program, 0, NULL, NULL, NULL, NULL);' in src
Exemple #8
0
def test_MakeDriver_optimizations_off():
    """Test that OpenCL optimizations are disabled when requested."""
    testcase = deepsmith_pb2.Testcase(inputs={
        "lsize": "1,1,1",
        "gsize": "1,1,1",
        "src": "kernel void A() {}"
    })
    src = cldrive.MakeDriver(testcase, False)
    print(src)
    assert "[cldrive] OpenCL optimizations: off" in src
    assert ('clBuildProgram(program, 0, NULL, "-cl-opt-disable", NULL, NULL);'
            in src)
Exemple #9
0
 def MakeDriverSrc(data_generator: str) -> str:
   testcase = deepsmith_pb2.Testcase(
     inputs={
       "gsize": f"{gsize_x},1,1",
       "lsize": f"{lsize_x},1,1",
       "data_generator": data_generator,
       "src": opencl_kernel,
     }
   )
   src = cldrive_harness.MakeDriver(testcase, optimizations=True)
   if testcase.invariant_opts["driver_type"] != "compile_and_run":
     raise DriverConstructionFailed(
       "Expected compile-and-run driver, found "
       + testcase.invariant_opts["driver_type"]
     )
   return src
Exemple #10
0
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