def multiply(self, m1, m2): '''Perform matrix multiplication between m1 and m2 using MapReduce''' product = [] for row in range(1, self.dim+1): for col in range(1, self.dim+1): #TODO: better ways to implement the multiplication op? map_fun = Code('''function() { for (i=0;i < this.elements.length; i++) { if((this.elements[i][0]== %s && this.key == '%s')|(this.elements[i][1]== %s && this.key=='%s')) { emit(null, this.elements[i]); } } }''' % (str(row), m1['key'], str(col), m2['key']) ) #assumes there is an order (is that okay?) red = Code('''function(key, values) { var total = 0; var dim = %s for(i=0; i < dim; i++) { total += values[i][2] * values[i+dim][2]; } return total; }''' % self.dim) result = self.matrix_coll.map_reduce(map_fun,red,'myresults') for doc in result.find(): product.append([row, col, int(doc['value'])]) print m1['key'], 'X', m2['key'] print_matrix(product)
def multiply(self, m1, m2): '''Perform matrix multiplication between m1 and m2 using MapReduce''' product = [] for row in range(1, self.dim+1): for col in range(1, self.dim+1): #TODO:how does the efficiency of this mapreduce compare?? query = self.client.add('matrices') query.map('''function(v) { var doc = JSON.parse(v.values[0].data); var matrix = new Array(); for (i=0; i < doc.elements.length; i++) { if((doc.elements[i][0] == %s && doc.key == '%s') | (doc.elements[i][1] == %s && doc.key == '%s')){ var match = {}; match[doc.key] = doc.elements[i]; matrix.push(match); } } return matrix; }''' % (str(row), m1['key'], str(col), m2['key'])) query.reduce('''function(data) { var m1 = new Array(); var m2 = new Array(); var m1_key = '%s'; var m2_key = '%s'; for (i=0;i<data.length;i++) { for(var key in data[i]) { if(key == m1_key) { m1.push(data[i]); } else { m2.push(data[i]); } } } var total = 0; for (i=0;i<m1.length;i++) { var val1 = m1[i][m1_key][2]; var val2 = m2[i][m2_key][2]; total += val1 * val2; } return [total]; }''' % (m1['key'],m2['key'])) #print row, m1['key'], col, m2['key'] for result in query.run(): product.append([row, col, result]) #print result print m1['key'], 'X', m2['key'] print_matrix(product)
def multiply(self, m1, m2): '''Perform matrix multiplication between m1 and m2 using MapReduce''' product = [] for row in range(1, self.dim+1): for col in range(1, self.dim+1): #maps proper row and column of m1 and m2 map_fun = ('''function(doc) { for (i=0;i < doc.elements.length; i++) { if((doc.elements[i][0]== %s && doc._id == '%s')|(doc.elements[i][1]== %s && doc._id=='%s')) { emit(null, doc.elements[i]); } } }''' % (str(row), m1.id, str(col), m2.id) ) #calculates dot product of the row and column red = ('''function(key, data) { var total = 0; var dim = %s for(i=0; i < dim; i++) { total += data[i][2] * data[i+dim][2]; } return total; }''' % self.dim) #creates a permanent view in Couch view = ViewDefinition('oper','multiply',map_fun,reduce_fun=red) view.sync(self.matrix_db) design = "_design/oper/_view/multiply" for result in self.matrix_db.view(design): product.append([row, col, result.value]) #prints the result print m1.id, 'X', m2.id print_matrix(product)
import pstats from riak_numbers import TestRiakNum from databases.print_matrix import print_matrix if __name__ == "__main__": for j in range(1, 4): riak_test = TestRiakNum() cProfile.run("riak_test.store_data()", "store{0}.profile".format(j)) cProfile.run("riak_test.test_retrieve()", "retrieve{0}.profile".format(j)) matrices = riak_test.test_retrieve() for i in range(0, len(matrices)): key = "matrix%s" % str(i + 1) for matrix in matrices: if matrix["key"] == key: print "Matrix %s:" % str(i + 1) print_matrix(matrix["elements"]) # cProfile.run('riak_test.test_analysis()','analysis{0}.profile'.format(j)) riak_test.remove_data() store = pstats.Stats("store1.profile") retr = pstats.Stats("retrieve1.profile") # analy = pstats.Stats('analysis1.profile') for i in range(2, 4): store.add("store{0}.profile".format(i)) retr.add("retrieve{0}.profile".format(i)) # analy.add('analysis{0}.profile'.format(i)) store.print_stats(5) retr.print_stats(5) # analy.print_stats(5)
import cProfile import pstats from redis_numbers import TestRedisNum from databases.print_matrix import print_matrix if __name__ == "__main__": """ Runs one cProfile test of couch, prints results. Results saved into file called <test_name>.profile (e.g. analysis.profile) """ redis_test = TestRedisNum() cProfile.run('redis_test.store_data()','store.profile') cProfile.run('redis_test.test_retrieve()','retrieve.profile') matrices = redis_test.test_retrieve() for i in range(0, len(matrices)): key = 'matrix%s' % str(i+1) for matrix in matrices: if matrix['key'] == key: print 'Matrix %s:' % str(i+1) print_matrix(matrix['elements']) cProfile.run('redis_test.remove_data()','remove.profile') store = pstats.Stats('store.profile') store.sort_stats('time').print_stats(5) retr = pstats.Stats('retrieve.profile') retr.sort_stats('time').print_stats(5)