Baunsgaard commented on code in PR #1983: URL: https://github.com/apache/systemds/pull/1983#discussion_r1460449522
########## src/main/java/org/apache/sysds/runtime/matrix/data/LibMatrixFourier.java: ########## @@ -0,0 +1,205 @@ +/* + * 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. + */ + +package org.apache.sysds.runtime.matrix.data; + +import org.apache.commons.math3.util.FastMath; + +public class LibMatrixFourier { + + /** + * Function to perform FFT on two given matrices. + * The first one represents the real values and the second one the imaginary values. + * The output also contains one matrix for the real and one for the imaginary values. + * + * @param re matrix object representing the real values + * @param im matrix object representing the imaginary values + * @return array of two matrix blocks + */ + public static MatrixBlock[] fft(MatrixBlock re, MatrixBlock im){ + + int rows = re.getNumRows(); + int cols = re.getNumColumns(); + if(!isPowerOfTwo(rows) || !isPowerOfTwo(cols)) throw new RuntimeException("false dimensions"); + + fft(re.getDenseBlockValues(), im.getDenseBlockValues(), rows, cols); + + return new MatrixBlock[]{re, im}; + } + + /** + * Function to perform IFFT on two given matrices. + * The first one represents the real values and the second one the imaginary values. + * The output also contains one matrix for the real and one for the imaginary values. + * + * @param re matrix object representing the real values + * @param im matrix object representing the imaginary values + * @return array of two matrix blocks + */ + public static MatrixBlock[] ifft(MatrixBlock re, MatrixBlock im){ + + int rows = re.getNumRows(); + int cols = re.getNumColumns(); + if(!isPowerOfTwo(rows) || !isPowerOfTwo(cols)) throw new RuntimeException("false dimensions"); + + ifft(re.getDenseBlockValues(), im.getDenseBlockValues(), rows, cols); + + return new MatrixBlock[]{re, im}; + } + + /** + * Function to perform FFT on two given double arrays. + * The first one represents the real values and the second one the imaginary values. + * Both arrays get updated and contain the result. + * + * @param re array representing the real values + * @param im array representing the imaginary values + * @param rows number of rows + * @param cols number of rows + */ + public static void fft(double[] re, double[] im, int rows, int cols) { + + double[] re_inter = new double[rows*cols]; + double[] im_inter = new double[rows*cols]; + + for(int i = 0; i < rows; i++){ + fft_one_dim(re, im, re_inter, im_inter, i*cols, 1, cols, cols); + } + + for(int j = 0; j < cols; j++){ + fft_one_dim(re, im, re_inter, im_inter, j, cols, rows, rows*cols); + } + + } + + /** + * Function to perform IFFT on two given double arrays. + * The first one represents the real values and the second one the imaginary values. + * Both arrays get updated and contain the result. + * + * @param re array representing the real values + * @param im array representing the imaginary values + * @param rows number of rows + * @param cols number of rows + */ + public static void ifft(double[] re, double[] im, int rows, int cols) { + + double[] re_inter = new double[rows*cols]; + double[] im_inter = new double[rows*cols]; + + for(int j = 0; j < cols; j++){ + ifft_one_dim(re, im, re_inter, im_inter, j, cols, rows, rows*cols); + } + + for(int i = 0; i < rows; i++){ + ifft_one_dim(re, im, re_inter, im_inter, i*cols, 1, cols, cols); + } + + } + + private static void fft_one_dim(double[] re, double[] im, double[] re_inter, double[] im_inter, int start, int step, int num, int subArraySize) { + + if(num == 1) return; + double angle = -2*FastMath.PI/num; + + // fft for even indices + fft_one_dim(re, im, re_inter, im_inter, start, step*2, num/2, subArraySize); + // fft for odd indices + fft_one_dim(re, im, re_inter, im_inter,start+step, step*2, num/2, subArraySize); Review Comment: Great! this solution should already be much faster, i am looking forward to see some performance numbers. To get even faster, and help java we can transform the "recursive code" into "iterative code". https://stackoverflow.com/questions/159590/way-to-go-from-recursion-to-iteration This helps for some java specific reasons, because each of your function calls gets put on top of the stack, it makes it hard for the JVM to JIT compile the method calls. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@systemds.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org