Skip to content

Commit

Permalink
Add prioritizer and budgeter.
Browse files Browse the repository at this point in the history
  • Loading branch information
thomcom committed May 12, 2023
1 parent 3c7ca1c commit b44ef89
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 0 deletions.
54 changes: 54 additions & 0 deletions modules/demo/api-server/test/plugins/prioritizer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) 2023, NVIDIA CORPORATION.
//
// 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.

'use strict'

const {Int32, Series} = require('@rapidsai/cudf');
const {test} = require('tap')
const Fastify = require('fastify')
const fixtures = require('../fixtures.js');
const prioritizer = require('../../util/prioritizer.js');

test('prioritizer initialize with points', async t => {
const points = Series.new([1, 2, 3, 4, 5]);
const result = new prioritizer(points);
t.same(result.points, points);
});

test('prioritizer set priorities', async t => {
const points = Series.new([1, 2, 3, 4, 5]);
const result = new prioritizer(points);
result.set_priorities(Series.new([1, 2, 3, 4, 4]));
t.same(result.point_machines.length, 4);
});

test('prioritizer get_n 1', async t => {
const points = Series.new([1, 2, 3, 4, 5]);
const result = new prioritizer(points);
result.set_priorities(Series.new([1, 2, 3, 4, 4]));
t.same(result.get_n(1), [1]);
t.same(result.get_n(1), [2]);
t.same(result.get_n(1), [3]);
t.same(result.get_n(1), [4]);
t.same(result.get_n(1), [5]);
});

test('prioritizer get_n 2', async t => {
const points = Series.new([1, 2, 3, 4, 5]);
const result = new prioritizer(points);
result.set_priorities(Series.new([1, 1, 1, 1, 1]));
t.same(result.get_n(2), [1, 3]);
t.same(result.get_n(2), [2, 4]);
t.same(result.get_n(2), [5]);
});
53 changes: 53 additions & 0 deletions modules/demo/api-server/util/budgeter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) 2023, NVIDIA CORPORATION.
//
// 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.

const {Int32, Series} = require('@rapidsai/cudf');

class Budgeter {
constructor(points) {
this.called = 0;
this.displayed_count = 0;
this._last_budget = null;
this.points = points;
}

get_n(budget) {
if (this._last_budget !== null && budget !== this._last_budget) {
// Budget was changed
this.called = 0;
}
const step = this.points.length / budget;
const indices =
Series.sequence({type: new Int32, init: 0, size: budget}).mul(step).add(this.called);
if (this.called === Math.floor(step)) {
// Return the final points that weren't previously sent.
const final_range =
Series.sequence({type: new Int32, init: this.displayed_count, size: this.points.length});
this.called++;
this.displayed_count += final_range.length;
return this.points.gather(final_range, true).dropNulls();
} else if (this.called > Math.floor(step)) {
// All out of points
this.called++;
return Series.new([]);
}
this.called++;
this.displayed_count += indices.length;
return this.points.gather(indices.cast(new Int32));
}

get_displayed_count() { return this.displayed_count; }
}

module.exports = Budgeter;
39 changes: 39 additions & 0 deletions modules/demo/api-server/util/prioritizer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) 2023, NVIDIA CORPORATION.
//
// 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.

const {Series} = require('@rapidsai/cudf');
const Budgeter = require('./budgeter');

class Prioritizer {
constructor(points) { this.points = points; }

set_priorities(priority_array) {
this.point_machines = [
new Budgeter(this.points.filter(priority_array.eq(1))),
new Budgeter(this.points.filter(priority_array.eq(2))),
new Budgeter(this.points.filter(priority_array.eq(3))),
new Budgeter(this.points.filter(priority_array.eq(4))),
];
}

get_n(budget) {
for (let i = 0; i < this.point_machines.length; i++) {
const prioritized = this.point_machines[i].get_n(budget);
if (prioritized.length > 0) { return prioritized; }
}
return new Series([]);
}
}

module.exports = Prioritizer;

0 comments on commit b44ef89

Please sign in to comment.