<commit_before>-- Copyright 2016 Stanford University
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
--     http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.

import "regent"

local c = regentlib.c
local std = terralib.includec("stdlib.h")
rawset(_G, "drand48", std.drand48)
rawset(_G, "srand48", std.srand48)

local CktConfig = require("circuit_config")

local helper = {}

local WIRE_SEGMENTS = 3
local DT = 1e-6

task helper.generate_random_circuit(rn : region(Node),
                                    rw : region(Wire(rn, rn, rn)),
                                    conf : CktConfig)
where reads writes(rn, rw)
do
  var piece_shared_nodes : &uint =
    [&uint](c.malloc([sizeof(uint)] * conf.num_pieces))
  for i = 0, conf.num_pieces do piece_shared_nodes[i] = 0 end

  srand48(conf.random_seed)

  var npieces = conf.num_pieces
  var npp = conf.nodes_per_piece
  var wpp = conf.wires_per_piece

  for p = 0, npieces do
    for i = 0, npp do
      var node = dynamic_cast(ptr(Node, rn), [ptr](p * npp + i))
      node.node_cap = drand48() + 1.0
      node.leakage = 0.1 * drand48()
      node.charge = 0.0
      node.node_voltage = 2 * drand48() - 1.0
    end
  end

  for p = 0, npieces do
    var ptr_offset = p * npp
    for i = 0, wpp do
      var wire = dynamic_cast(ptr(Wire(rn, rn, rn), rw), [ptr](p * wpp + i))
      wire.current.{_0, _1, _2} = 0.0
      wire.voltage.{_0, _1} = 0.0
      wire.resistance = drand48() * 10.0 + 1.0

      -- Keep inductance on the order of 1e-3 * dt to avoid resonance problems
      wire.inductance = (drand48() + 0.1) * DT * 1e-3
      wire.wire_cap = drand48() * 0.1

      var in_node = ptr_offset + [uint](drand48() * npp)
      wire.in_ptr = dynamic_cast(ptr(Node, rn, rn), [ptr](in_node))
      regentlib.assert(not isnull(wire.in_ptr),
        "picked an invalid random pointer")

      var out_node = 0
      if (100 * drand48() < conf.pct_wire_in_piece) or (npieces == 1) then
        out_node = ptr_offset + [uint](drand48() * npp)
      else
        -- pick a random other piece and a node from there
        var pp = [uint](drand48() * (conf.num_pieces - 1))
        if pp >= p then pp += 1 end

        -- pick an arbitrary node, except that if it's one that didn't used to be shared, make the
        -- sequentially next pointer shared instead so that each node's shared pointers stay compact
        var idx = [uint](drand48() * npp)
        if idx > piece_shared_nodes[pp] then
          idx = piece_shared_nodes[pp]
          piece_shared_nodes[pp] = piece_shared_nodes[pp] + 1
        end
        out_node = pp * npp + idx
      end
      wire.out_ptr = dynamic_cast(ptr(Node, rn, rn, rn), [ptr](out_node))
      regentlib.assert(not isnull(wire.out_ptr),
        "picked an invalid random pointer within a piece")
    end
  end
  c.free(piece_shared_nodes)
end

task helper.validate_pointers(rpn : region(Node),
                              rsn : region(Node),
                              rgn : region(Node),
                              rw : region(Wire(rpn, rsn, rgn)))
where reads(rpn, rsn, rgn),
      reads writes(rw.{in_ptr, out_ptr})
do
  for w in rw do
    w.in_ptr = dynamic_cast(ptr(Node, rpn, rsn), w.in_ptr)
    if isnull(w.in_ptr) then
      c.printf("validation error: wire %d does not originate from the piece it belongs\n",
        __raw(w))
      regentlib.assert(false, "pointer validation failed")
    end
    w.out_ptr = dynamic_cast(ptr(Node, rpn, rsn, rgn), w.out_ptr)
    if isnull(w.out_ptr) then
      c.printf("validation error: wire %d points to an invalid nod\n",
        __raw(w))
      regentlib.assert(false, "pointer validation failed")
    end
  end
end

task helper.dump_graph(conf : CktConfig,
                       rn : region(Node),
                       rw : region(Wire(rn, rn, rn)),
                       pn_private : partition(disjoint, rn),
                       pn_shared : partition(disjoint, rn),
                       pn_ghost : partition(aliased, rn),
                       pw_outgoing : partition(disjoint, rw))
where reads(rn, rw)
do
  for i = 0, conf.num_pieces do
    var rpn = pn_private[i]
    var rsn = pn_shared[i]
    var rgn = pn_ghost[i]<commit_msg>regent: fixing a typo in an example<commit_after>-- Copyright 2016 Stanford University
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
--     http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.

import "regent"

local c = regentlib.c
local std = terralib.includec("stdlib.h")
rawset(_G, "drand48", std.drand48)
rawset(_G, "srand48", std.srand48)

local CktConfig = require("circuit_config")

local helper = {}

local WIRE_SEGMENTS = 3
local DT = 1e-6

task helper.generate_random_circuit(rn : region(Node),
                                    rw : region(Wire(rn, rn, rn)),
                                    conf : CktConfig)
where reads writes(rn, rw)
do
  var piece_shared_nodes : &uint =
    [&uint](c.malloc([sizeof(uint)] * conf.num_pieces))
  for i = 0, conf.num_pieces do piece_shared_nodes[i] = 0 end

  srand48(conf.random_seed)

  var npieces = conf.num_pieces
  var npp = conf.nodes_per_piece
  var wpp = conf.wires_per_piece

  for p = 0, npieces do
    for i = 0, npp do
      var node = dynamic_cast(ptr(Node, rn), [ptr](p * npp + i))
      node.node_cap = drand48() + 1.0
      node.leakage = 0.1 * drand48()
      node.charge = 0.0
      node.node_voltage = 2 * drand48() - 1.0
    end
  end

  for p = 0, npieces do
    var ptr_offset = p * npp
    for i = 0, wpp do
      var wire = dynamic_cast(ptr(Wire(rn, rn, rn), rw), [ptr](p * wpp + i))
      wire.current.{_0, _1, _2} = 0.0
      wire.voltage.{_0, _1} = 0.0
      wire.resistance = drand48() * 10.0 + 1.0

      -- Keep inductance on the order of 1e-3 * dt to avoid resonance problems
      wire.inductance = (drand48() + 0.1) * DT * 1e-3
      wire.wire_cap = drand48() * 0.1

      var in_node = ptr_offset + [uint](drand48() * npp)
      wire.in_ptr = dynamic_cast(ptr(Node, rn, rn), [ptr](in_node))
      regentlib.assert(not isnull(wire.in_ptr),
        "picked an invalid random pointer")

      var out_node = 0
      if (100 * drand48() < conf.pct_wire_in_piece) or (npieces == 1) then
        out_node = ptr_offset + [uint](drand48() * npp)
      else
        -- pick a random other piece and a node from there
        var pp = [uint](drand48() * (conf.num_pieces - 1))
        if pp >= p then pp += 1 end

        -- pick an arbitrary node, except that if it's one that didn't used to be shared, make the
        -- sequentially next pointer shared instead so that each node's shared pointers stay compact
        var idx = [uint](drand48() * npp)
        if idx > piece_shared_nodes[pp] then
          idx = piece_shared_nodes[pp]
          piece_shared_nodes[pp] = piece_shared_nodes[pp] + 1
        end
        out_node = pp * npp + idx
      end
      wire.out_ptr = dynamic_cast(ptr(Node, rn, rn, rn), [ptr](out_node))
      regentlib.assert(not isnull(wire.out_ptr),
        "picked an invalid random pointer within a piece")
    end
  end
  c.free(piece_shared_nodes)
end

task helper.validate_pointers(rpn : region(Node),
                              rsn : region(Node),
                              rgn : region(Node),
                              rw : region(Wire(rpn, rsn, rgn)))
where reads(rpn, rsn, rgn),
      reads writes(rw.{in_ptr, out_ptr})
do
  for w in rw do
    w.in_ptr = dynamic_cast(ptr(Node, rpn, rsn), w.in_ptr)
    if isnull(w.in_ptr) then
      c.printf("validation error: wire %d does not originate from the piece it belongs\n",
        __raw(w))
      regentlib.assert(false, "pointer validation failed")
    end
    w.out_ptr = dynamic_cast(ptr(Node, rpn, rsn, rgn), w.out_ptr)
    if isnull(w.out_ptr) then
      c.printf("validation error: wire %d points to an invalid node\n",
        __raw(w))
      regentlib.assert(false, "pointer validation failed")
    end
  end
end

task helper.dump_graph(conf : CktConfig,
                       rn : region(Node),
                       rw : region(Wire(rn, rn, rn)),
                       pn_private : partition(disjoint, rn),
                       pn_shared : partition(disjoint, rn),
                       pn_ghost : partition(aliased, rn),
                       pw_outgoing : partition(disjoint, rw))
where reads(rn, rw)
do
  for i = 0, conf.num_pieces do
    var rpn = pn_private[i]
    var rsn = pn_shared[i]
    var rgn = pn_ghost[i]
{'fraction_non_alphanumeric': 0.11170841844962907, 'fraction_numerical': 0.019137727126115472, 'mean_word_length': 2.3544897223223944, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4369789', 'n_tokens_mistral': 3281, 'n_tokens_neox': 3074, 'n_words': 943}