• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <cstdint>
18 #include <memory>
19 #include <utility>
20 #include <vector>
21 
22 #include "fcp/aggregation/core/datatype.h"
23 #include "fcp/aggregation/core/mutable_vector_data.h"
24 #include "fcp/aggregation/core/tensor.h"
25 #include "fcp/aggregation/core/tensor_aggregator_factory.h"
26 #include "fcp/aggregation/core/tensor_aggregator_registry.h"
27 #include "fcp/aggregation/core/tensor_shape.h"
28 
29 // Open-source version of benchmarking library
30 #include "benchmark/benchmark.h"
31 
32 namespace fcp {
33 namespace aggregation {
34 namespace {
35 
36 constexpr static int64_t kLength = 1000000;
37 
BM_FederatedSumAccumulate(benchmark::State & state)38 static void BM_FederatedSumAccumulate(benchmark::State& state) {
39   auto aggregator = (*GetAggregatorFactory("federated_sum"))
40                         ->Create(DT_INT64, {kLength})
41                         .value();
42   auto test_data = std::make_unique<MutableVectorData<int64_t>>(kLength);
43   std::vector<int64_t>& input = *test_data;
44   for (int64_t i = 0; i < kLength; ++i) {
45     input[i] = i % 123;
46   }
47   auto tensor = Tensor::Create(DT_INT64, {kLength}, std::move(test_data));
48   auto items_processed = 0;
49   for (auto s : state) {
50     benchmark::DoNotOptimize(aggregator->Accumulate(*tensor));
51     items_processed += kLength;
52   }
53   state.SetItemsProcessed(items_processed);
54 }
55 
56 BENCHMARK(BM_FederatedSumAccumulate);
57 
58 }  // namespace
59 }  // namespace aggregation
60 }  // namespace fcp
61