Skip to content

Commit

Permalink
update optimizing_tape
Browse files Browse the repository at this point in the history
  • Loading branch information
pca006132 committed Dec 29, 2024
1 parent 5ad914f commit 9716bc3
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 32 deletions.
42 changes: 42 additions & 0 deletions src/sdf/affine_value.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2024 The Manifold Authors.
//
// 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.
#pragma once

#include "context.h"

namespace manifold::sdf {
struct AffineValue {
// value = var * a + b
Operand var;
double a;
double b;

AffineValue(Operand var, double a, double b) : var(var), a(a), b(b) {}
AffineValue(double constant) : var(Operand::none()), a(0.0), b(constant) {}
bool operator==(const AffineValue &other) const {

Check warning on line 27 in src/sdf/affine_value.h

View check run for this annotation

Codecov / codecov/patch

src/sdf/affine_value.h#L26-L27

Added lines #L26 - L27 were not covered by tests
return var == other.var && a == other.a && b == other.b;
}
AffineValue operator+(double d) { return AffineValue(var, a, b + d); }
AffineValue operator*(double d) { return AffineValue(var, a * d, b * d); }
};
} // namespace manifold::sdf

template <>
struct std::hash<AffineValue> {
size_t operator()(const AffineValue &value) const {

Check warning on line 37 in src/sdf/affine_value.h

View check run for this annotation

Codecov / codecov/patch

src/sdf/affine_value.h#L37

Added line #L37 was not covered by tests
size_t h = std::hash<int>()(value.var.id);
hash_combine(h, value.a, value.b);
return h;
}
};
25 changes: 1 addition & 24 deletions src/sdf/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,9 @@
#include <iostream>
#endif

#include "affine_value.h"
#include "manifold/optional_assert.h"

struct AffineValue {
// value = var * a + b
Operand var;
double a;
double b;

AffineValue(Operand var, double a, double b) : var(var), a(a), b(b) {}
AffineValue(double constant) : var(Operand::none()), a(0.0), b(constant) {}
bool operator==(const AffineValue &other) const {
return var == other.var && a == other.a && b == other.b;
}
AffineValue operator+(double d) { return AffineValue(var, a, b + d); }
AffineValue operator*(double d) { return AffineValue(var, a * d, b * d); }
};

template <>
struct std::hash<AffineValue> {
size_t operator()(const AffineValue &value) const {
size_t h = std::hash<int>()(value.var.id);
hash_combine(h, value.a, value.b);
return h;
}
};

namespace manifold::sdf {
void Context::dump() const {

Check warning on line 27 in src/sdf/context.cpp

View check run for this annotation

Codecov / codecov/patch

src/sdf/context.cpp#L27

Added line #L27 was not covered by tests
#ifdef MANIFOLD_DEBUG
Expand Down
1 change: 0 additions & 1 deletion src/sdf/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ using namespace manifold::sdf;

inline void hash_combine(std::size_t& seed) {}

Check warning on line 58 in src/sdf/context.h

View check run for this annotation

Codecov / codecov/patch

src/sdf/context.h#L58

Added line #L58 was not covered by tests

// note: ankerl hash combine function is too costly
template <typename T, typename... Rest>
inline void hash_combine(std::size_t& seed, const T& v, Rest... rest) {
std::hash<T> hasher;
Expand Down
14 changes: 7 additions & 7 deletions src/sdf/optimizing_tape.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include <vector>

#include "affine_value.h"
#include "interval.h"
#include "manifold/vec_view.h"

Expand Down Expand Up @@ -58,7 +59,7 @@ class OptimizerContext {
* For dependencies, we use relative index to track them.
* ID = current ID - value
* - If value is 0, this means the operand is not being used, i.e. the
* instruction does not have 3 operands.
* instruction does not have that operand.
* - If value is 255, this means the dependency is too far away, and we
* should look it up in far dependencies.
* Ideally, we should not have too many far dependencies.
Expand All @@ -80,19 +81,18 @@ class OptimizerContext {
* memory operations, we reuse them.
*
* - `buffer` is the regular register buffer for tape evaluation.
* - `constantOffset` is a constant that adds to a corresponding register.
* This can be constant folded.
* - `results` contains instruction id + register id, indicating the
* predetermined branch result for choice/min/max function.
* - `affineValue` is the affine value associated with a register.
* - `results` contains instruction id + affine value, indicating the
* optimized result for some instruction.
* Note that this must be sorted according to instruction id.
* - `uses` is the temporary use count vector that is mutable in each
* evaluation. It is reset before each evaluation.
* - `dead` contains instruction IDs that are dead, for later dead code
* elimination.
*/
VecView<Interval<double>> buffer;
VecView<double> constantOffset;
std::vector<std::pair<uint32_t, uint8_t>> results;
VecView<AffineValue> affineValues;
std::vector<std::pair<uint32_t, AffineValue>> results;
std::vector<uint8_t> uses;
std::vector<uint32_t> dead;
};
Expand Down

0 comments on commit 9716bc3

Please sign in to comment.