#-------------------------------------------------------------
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you 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.
#
#-------------------------------------------------------------

D = rand(rows=32, cols=100, min=0, max=20, seed=42)
bs = 32;
ep = 3;
iter_ep = ceil(nrow(D)/bs);
maxiter = ep * iter_ep;
beg = 1;
iter = 0;
i = 1;
R = matrix(0, rows=1, cols=maxiter+1);

while (iter < maxiter) {
  end = beg + bs - 1;
  if (end>nrow(D))
    end = nrow(D);
  X = D[beg:end,]

  #inlace binary after inplace indexing corrupts the dataset
  R[1,iter+1] = sum(D);

  #reusable OP across epochs
  X = scale(X, FALSE, TRUE);
  #pollute cache with not reusable OPs
  X = ((X + X) * i - X) / (i+1)
  X = ((X + X) * i - X) / (i+1)
  X = ((X + X) * i - X) / (i+1)
  X = ((X + X) * i - X) / (i+1)
  X = ((X + X) * i - X) / (i+1)
  X = ((X + X) * i - X) / (i+1)
  X = ((X + X) * i - X) / (i+1)
  X = ((X + X) * i - X) / (i+1)
  X = ((X + X) * i - X) / (i+1)
  X = ((X + X) * i - X) / (i+1)

  iter = iter + 1;
  if (end == nrow(D))
    beg = 1;
  else
    beg = end + 1;
  i = i + 1;

}
#R = X;
R[1,maxiter+1] = sum(X);
write(R, $1, format="text");

