[pve-devel] [PATCH v2 pve-manager 2/7] doproduct: add alternatives method

Alexandre Derumier aderumier at odiso.com
Wed Oct 9 16:01:32 CEST 2019


from:
  https://hal.archives-ouvertes.fr/hal-00868016v2/document
Signed-off-by: Alexandre Derumier <aderumier at odiso.com>
---
 PVE/API2/Nodes.pm | 23 ++++++++++++++++++++---
 1 file changed, 20 insertions(+), 3 deletions(-)

diff --git a/PVE/API2/Nodes.pm b/PVE/API2/Nodes.pm
index 303a7ffb..9202932a 100644
--- a/PVE/API2/Nodes.pm
+++ b/PVE/API2/Nodes.pm
@@ -2000,7 +2000,7 @@ __PACKAGE__->register_method ({
 			# fixme: check vmbr available
 
 			my @vec_node = ($node_freecpu, $node_freemem); #? add network usage dimension ?
-			my $weight = dotprod(\@vec_vm,\@vec_node);
+			my $weight = dotprod(\@vec_vm,\@vec_node, 'normR');
 			$nodes_weight->{$weight} = $node;
 			$highest_weight = $weight if $weight > $highest_weight;
 		    }	
@@ -2131,10 +2131,27 @@ sub complete_templet_repo {
 }
 
 sub dotprod {
-    my ($vec_a, $vec_b) = @_;
+    my($vec_a, $vec_b, $mode) = @_;
     die "they must have the same size\n" unless @$vec_a == @$vec_b;
+
     my $sum = 0;
-    $sum += $vec_a->[$_] * $vec_b->[$_] for 0..$#$vec_a;
+    my $norm_a = 0;
+    my $norm_b = 0;
+
+    for(my $i=0; $i < scalar @{$vec_a}; $i++) {
+	my $a = @{$vec_a}[$i];
+	my $b = @{$vec_b}[$i];
+
+	$sum += $a * $b;
+	$norm_a += $a * $a;
+	$norm_b += $b * $b;
+    }
+
+    if($mode eq 'normR') {
+	return $sum / (sqrt($norm_a) * sqrt($norm_b))
+    } elsif ($mode eq 'normC') {
+	return $sum / $norm_b;
+    }
     return $sum;
 }
 
-- 
2.20.1




More information about the pve-devel mailing list