package edu.du.bangert.assign4.cachesim; import java.util.Random; public class CacheGrader { // static variables public final static int BYTES_PER_KB = 1024; public final static int MEMORY_SIZE = 512...

Need to complete the Cache.java file so that it passes the CacheGrader.java. Only Cache.java should be edited, other files are already completed.


package edu.du.bangert.assign4.cachesim; import java.util.Random; public class CacheGrader { // static variables public final static int BYTES_PER_KB = 1024; public final static int MEMORY_SIZE = 512 * BYTES_PER_KB; public final static Random s_rand = new Random(0); // instance variables private MockMemory m_memory; private int m_nAssertions = 0; private int m_nFailedAssertions = 0; public static void main(String[] args) { new CacheGrader().run(); } private ReadOnlyCache newInstance(int blockCount, int bytesPerBlock, int associativity) { // new memory for each new Cache object. m_memory = new MockMemory(s_rand, MEMORY_SIZE); return new Cache(m_memory, blockCount, bytesPerBlock, associativity); } private void run() { testDirectMapped(8, 32); testDirectMapped(32, 8); testDirectMapped(16, 4); testDirectMapped(64, 2); testDirectMapped(32, 2); int dmAssertions = m_nAssertions, dmFailedAssertions = m_nFailedAssertions; m_nAssertions = m_nFailedAssertions = 0; testFullyAssociative(2, 4); testFullyAssociative(4, 2); testFullyAssociative(8, 32); testFullyAssociative(32, 8); testFullyAssociative(32, 64); testFullyAssociative(64, 32); int faAssertions = m_nAssertions, faFailedAssertions = m_nFailedAssertions; m_nAssertions = m_nFailedAssertions = 0; testSetAssociative(4, 4, 2); testSetAssociative(8, 8, 4); testSetAssociative(16, 8, 8); testSetAssociative(16, 16, 2); testSetAssociative(512, 8, 256); int saAssertions = m_nAssertions, saFailedAssertions = m_nFailedAssertions; m_nAssertions = m_nFailedAssertions = 0; // Direct Mapped - 40% of grade, Fully associative - 30%, Set Associative - 30% // of grade int dmPassedAssertions = (dmAssertions - dmFailedAssertions); System.err.println(String.format("DIRECT MAPPED: PASSED %d/%d = %f%%", dmPassedAssertions, dmAssertions, (100.0 * dmPassedAssertions) / ((double) dmAssertions))); int faPassedAssertions = (faAssertions - faFailedAssertions); System.err.println(String.format("FULLY ASSOCIATIVE: PASSED %d/%d = %f%%", faPassedAssertions, faAssertions, (100.0 * faPassedAssertions) / ((double) faAssertions))); int saPassedAssertions = (saAssertions - saFailedAssertions); System.err.println(String.format("SET ASSOCIATIVE: PASSED %d/%d = %f%%", saPassedAssertions, saAssertions, (100.0 * saPassedAssertions) / ((double) saAssertions))); System.err.println(String.format("OVERALL SCORE (weighted) = %f%%", 100.0 * (0.40 * dmPassedAssertions / ((double) dmAssertions) + 0.30 * faPassedAssertions / ((double) faAssertions) + 0.30 * saPassedAssertions / ((double) saAssertions)))); } private void testFullyAssociative(int nBlocks, int nBytesPerBlock) { final String THIS_METHOD = new Object() { }.getClass().getEnclosingMethod().getName(); // fully associative (associativity == # blocks) ReadOnlyCache cache = newInstance(nBlocks, nBytesPerBlock, nBlocks); assertEquals(THIS_METHOD, "checking zero reads from memory initially.", 0, m_memory.getReadCalls()); // access 1st block for (int n = 0; n < nbytesperblock;="" n++)="" {="" int="" i="s_rand.nextInt(nBytesPerBlock);" byte="" value="cache.load(i);" assertequals(this_method,="" string.format("mem[%d]="" value",="" i),="" m_memory.peek(i),="" value,="" true);="" }="" assertequals(this_method,="" "1="" memory="" reads="" after="" accessing="" first="" block",="" 1,="" m_memory.getreadcalls());="" access="" 2nd="" block="" for="" (int="" n="0;" n="">< nbytesperblock;="" n++)="" {="" int="" i="nBytesPerBlock" +="" s_rand.nextint(nbytesperblock);="" byte="" value="cache.load(i);" assertequals(this_method,="" string.format("mem[%d]="" value",="" i),="" m_memory.peek(i),="" value,="" true);="" }="" assertequals(this_method,="" "2="" memory="" reads="" after="" accessing="" 2nd="" block",="" 2,="" m_memory.getreadcalls());="" access="" first="" block="" again="" --="" this="" should="" update="" the="" lru="" of="" the="" 1st="" block="" for="" (int="" n="0;" n="">< nbytesperblock;="" n++)="" {="" int="" i="s_rand.nextInt(nBytesPerBlock);" byte="" value="cache.load(i);" assertequals(this_method,="" string.format("mem[%d]="" value",="" i),="" m_memory.peek(i),="" value,="" true);="" }="" assertequals(this_method,="" "2="" memory="" reads="" after="" accessing="" first="" block="" again",="" 2,="" m_memory.getreadcalls());="" access="" 3rd="" block="" up="" to="" n="" to="" fill="" up="" the="" cache="" for="" (int="" b="2;" b="">< nblocks;="" b++)="" {="" for="" (int="" n="0;" n="">< nbytesperblock;="" n++)="" {="" int="" i="b" *="" nbytesperblock="" +="" s_rand.nextint(nbytesperblock);="" byte="" value="cache.load(i);" assertequals(this_method,="" string.format("mem[%d]="" value",="" i),="" m_memory.peek(i),="" value,="" true);="" }="" assertequals(this_method,="" "another="" memory="" reads="" after="" accessing="" next",="" (b="" +="" 1),="" m_memory.getreadcalls());="" }="" now="" access="" the="" very="" next="" block="" a="" bunch="" of="" times="" for="" (int="" n="0;" n="">< nbytesperblock;="" n++)="" {="" int="" i="nBlocks" *="" nbytesperblock="" +="" s_rand.nextint(nbytesperblock);="" byte="" value="cache.load(i);" assertequals(this_method,="" string.format("mem[%d]="" value",="" i),="" m_memory.peek(i),="" value,="" true);="" }="" assertequals(this_method,="" "after="" access="" overflow="" block",="" nblocks="" +="" 1,="" m_memory.getreadcalls());="" access="" first="" block="" again,="" should="" not="" have="" been="" kicked="" out.="" for="" (int="" n="0;" n="">< nbytesperblock;="" n++)="" {="" int="" i="s_rand.nextInt(nBytesPerBlock);" byte="" value="cache.load(i);" assertequals(this_method,="" string.format("mem[%d]="" value",="" i),="" m_memory.peek(i),="" value,="" true);="" }="" assertequals(this_method,="" "after="" accessing="" 1st="" block="" again",="" nblocks="" +="" 1,="" m_memory.getreadcalls());="" access="" 2nd="" block="" again,="" this="" should="" have="" been="" kicked="" out="" as="" lru,="" so="" accessing="" it="" again="" will="" require="" another="" memory="" access.="" for="" (int="" n="0;" n="">< nbytesperblock;="" n++)="" {="" int="" i="nBytesPerBlock" +="" s_rand.nextint(nbytesperblock);="" byte="" value="cache.load(i);" assertequals(this_method,="" string.format("mem[%d]="" value",="" i),="" m_memory.peek(i),="" value,="" true);="" }="" assertequals(this_method,="" "after="" accessing="" 2rd="" block="" again",="" nblocks="" +="" 2,="" m_memory.getreadcalls());="" }="" private="" void="" testsetassociative(int="" nblocks,="" int="" nbytesperblock,="" int="" associativity)="" {="" final="" string="" this_method="new" object()="" {="" }.getclass().getenclosingmethod().getname();="" 4="" blocks,="" 4="" bytes/block,="" 2-associative="" (4/2="2)" so="" there="" are="" two="" sets:="" set="" 0:="" (0,1,2,3)="" (8,9,10,11)="" (16,17,18,19)="" ...="" set="" 1:="" (4,5,6,7)="" (12,13,14,15)="" (20,21,22,23)="" ...="" readonlycache="" cache="newInstance(nBlocks," nbytesperblock,="" associativity);="" assertequals(this_method,="" "checking="" zero="" reads="" from="" memory="" initially.",="" 0,="" m_memory.getreadcalls());="" int="" nsets="nBlocks" associativity;="" access="" all="" the="" blocks="" in="" the="" first="" set="" for="" (int="" a="0;" a="">< associativity;="" a++)="" {="" for="" (int="" n="0;" n="">< nbytesperblock;="" n++)="" {="" int="" i="a" *="" nsets="" *="" nbytesperblock="" +="" s_rand.nextint(nbytesperblock);="" byte="" value="cache.load(i);" assertequals(this_method,="" string.format("mem[%d]="" value",="" i),="" m_memory.peek(i),="" value,="" true);="" }="" assertequals(this_method,="" "another="" memory="" reads="" after="" next="" set",="" a="" +="" 1,="" m_memory.getreadcalls());="" }="" int="" expectedreads="m_memory.getReadCalls();" save="" the="" number="" of="" memory="" access="" at="" this="" point="" now="" go="" back="" through="" those="" blocks="" again="" for="" (int="" a="0;" a="">< associativity;="" a++)="" {="" for="" (int="" n="0;" n="">< nbytesperblock;="" n++)="" {="" int="" i="a" *="" nsets="" *="" nbytesperblock="" +="" s_rand.nextint(nbytesperblock);="" byte="" value="cache.load(i);" assertequals(this_method,="" string.format("mem[%d]="" value",="" i),="" m_memory.peek(i),="" value,="" true);="" }="" assertequals(this_method,="" "no="" more="" reads="" after="" accessing="" first="" set="" again",="" expectedreads,="" m_memory.getreadcalls());="" }="" access="" the="" 2nd="" block,="" this="" should="" go="" in="" the="" other="" set="" and="" generate="" one="" more="" memory="" access="" expectedreads++;="" for="" (int="" n="0;" n="">< nbytesperblock;="" n++)="" {="" int="" i="nBytesPerBlock" +="" s_rand.nextint(nbytesperblock);="" byte="" value="cache.load(i);" assertequals(this_method,="" string.format("mem[%d]="" value",="" i),="" m_memory.peek(i),="" value,="" true);="" }="" assertequals(this_method,="" "additional="" memory="" read="" after="" accessing="" 2nd="" block",="" expectedreads,="" m_memory.getreadcalls());="" access="" first="" block="" --="" should="" not="" have="" been="" kicked="" out="" for="" (int="" n="0;" n="">< nbytesperblock;="" n++)="" {="" int="" i="s_rand.nextInt(nBytesPerBlock);" byte="" value="cache.load(i);" assertequals(this_method,="" string.format("mem[%d]="" value",="" i),="" m_memory.peek(i),="" value,="" true);="" }="" assertequals(this_method,="" "no="" additional="" memory="" read="" after="" accessing="" 1st="" block",="" expectedreads,="" m_memory.getreadcalls());="" at="" this="" point="" we="" have="" the="" following:="" set="" 0="" is="" full="" set="" 1="" has="" only="" its="" 1st="" entry="" filled="" access="" set="" 0="" again="" with="" a="" new="" different="" block,="" and="" the="" 2nd="" block="" in="" set="" 0="" should="" have="" been="" kicked="" out="" expectedreads++;="" for="" (int="" n="0;" n="">< nbytesperblock;="" n++)="" {="" int="" i="associativity" *="" nsets="" *="" nbytesperblock="" +="" s_rand.nextint(nbytesperblock);="" byte="" value="cache.load(i);" assertequals(this_method,="" string.format("mem[%d]="" value",="" i),="" m_memory.peek(i),="" value,="" true);="" }="" assertequals(this_method,="" "one="" more="" memory="" read="" after="" accessing="" set="" 0",="" expectedreads,="" m_memory.getreadcalls());="" access="" first="" block="" --="" should="" not="" have="" been="" kicked="" out="" for="" (int="" n="0;" n="">< nbytesperblock;="" n++)="" {="" int="" i="s_rand.nextInt(nBytesPerBlock);" byte="" value="cache.load(i);" assertequals(this_method,="" string.format("mem[%d]="" value",="" i),="" m_memory.peek(i),="" value,="" true);="" }="" assertequals(this_method,="" "no="" additional="" memory="" read="" after="" accessing="" 1st="" block="" for="" a="" 3rd="" time",="" expectedreads,="" m_memory.getreadcalls());="" access="" 2nd="" set="" --="" should="" not="" have="" been="" kicked="" out="" for="" (int="" n="0;" n="">< nbytesperblock;="" n++)="" {="" int="" i="nBytesPerBlock" +="" s_rand.nextint(nbytesperblock);="" byte="" value="cache.load(i);" assertequals(this_method,="" string.format("mem[%d]="" value",="" i),="" m_memory.peek(i),="" value,="" true);="" }="" assertequals(this_method,="" "no="" additional="" memory="" read="" after="" accessing="" 2st="" block="" again",="" expectedreads,="" m_memory.getreadcalls());="" access="" 2nd="" block="" in="" 1st="" set,="" this="" should="" require="" another="" memory="" access="" expectedreads++;="" for="" (int="" n="0;" n="">< nbytesperblock;="" n++)="" {="" int="" i="1" *="" nsets="" *="" nbytesperblock="" +="" s_rand.nextint(nbytesperblock);="" byte="" value="cache.load(i);" assertequals(this_method,="" string.format("mem[%d]="" value",="" i),="" m_memory.peek(i),="" value,="" true);="" }="" assertequals(this_method,="" "no="" additional="" memory="" read="" after="" accessing="" 2st="" block="" again",="" expectedreads,="" m_memory.getreadcalls());="" }="" private="" void="" testdirectmapped(int="" nblocks,="" int="" nbytesperblock)="" {="" final="" string="" this_method="String.format("%s(%d,%d)"," new="" object()="" {="" }.getclass().getenclosingmethod().getname(),="" nblocks,="" nbytesperblock);="" set="" up="" some="" constants="" for="" this="" method="" final="" int="" associativity="1;" int="" nelements="nBlocks" *="" nbytesperblock;="" readonlycache="" cache="newInstance(nBlocks," nbytesperblock,="" associativity);="" pick="" a="" random="" start="" point="" --="" guaranteed="" to="" be="" a="" multiple="" of="" nbytesperblock="" int="" base="nBytesPerBlock" *="" (1="" +="" s_rand.nextint(1024));="" do="" this="" a="" bunch="" of="" times,="" should="" only="" require="" one="" read="" per="" block,="" read="" sequentially="" through="" a="" chunk="" of="" memory="" for="" (int="" t="0;" t="">< 3;="" t++)="" {="" for="" (int="" i="0;" i="">< nelements;="" i++)="" {="" int="" address="base" +="" i;="" check="" that="" the="" cache="" provides="" the="" right="" values="" byte="" value="cache.load(address);" assertequals(this_method,="" "sequential="" value="" test",="" m_memory.peek(address),="" value,="" true);="" }="" }="" now="" do="" some="" random="" accesses="" within="" the="" range="" for="" (int="" t="0;" t="">< 10;="" t++)="" {="" int="" address="base" +="" s_rand.nextint(nelements);="" assertequals(this_method,="" "random="" value="" test",="" m_memory.peek(address),="" cache.load(address));="" }="" after="" all="" of="" that="" this="" should="" have="" just="" required="" one="" read="" per="" block,="" and="" each="" address="" should="" have="" been="" read="" exactly="" once="" assertequals(this_method,="" "total="" reads",="" nblocks,="" m_memory.getreadcalls());="" for="" (int="" i="0;" i="">< nelements;="" i++)="" {="" assertequals(this_method,="" "per-address="" reads",="" 1,="" m_memory.getreadcount(base="" +="" i));="" }="" go="" a="" specific="" address="" in="" the="" next="" chunk="" of="" memory,="" guaranteed="" to="" not="" have="" been="" loaded="" already="" int="" element2="s_rand.nextInt(nElements);" int="" address2="base" +="" nelements="" +="" element2;="" go="" to="" some="" random="" offset="" assertequals(this_method,="" "2nd="" chunk",="" m_memory.peek(address2),="" cache.load(address2));="" assertequals(this_method,="" "2nd="" chunk="" reads",="" nblocks="" +="" 1,="" m_memory.getreadcalls());="" each="" of="" the="" addresses="" in="" this="" chunk="" should="" have="" been="" read="" exactly="" once="" for="" (int="" i="0;" i="">< nbytesperblock;="" i++)="" {="" int="" address="base" +="" nelements="" +="" (element2="" -="" (element2="" %="" nbytesperblock))="" +="" i;="" assertequals(this_method,="" "2nd="" chunk="" per-address="" read="" counts",="" 1,="" m_memory.getreadcount(address));="" }="" each="" of="" the="" addresses="" in="" the="" original="" block="" should="" not="" have="" been="" read="" any="" more="" for="" (int="" i="0;" i="">< nelements; i++) { assertequals(this_method, "per-address reads post 2nd chunk", 1, m_memory.getreadcount(base + i)); } // now load the same element from the original chunk, should result in that // previous block being read again int address3 = base + element2; assertequals(this_method, "back to original", m_memory.peek(address3), cache.load(address3)); assertequals(this_method, "back to original reads", nblocks nelements;="" i++)="" {="" assertequals(this_method,="" "per-address="" reads="" post="" 2nd="" chunk",="" 1,="" m_memory.getreadcount(base="" +="" i));="" }="" now="" load="" the="" same="" element="" from="" the="" original="" chunk,="" should="" result="" in="" that="" previous="" block="" being="" read="" again="" int="" address3="base" +="" element2;="" assertequals(this_method,="" "back="" to="" original",="" m_memory.peek(address3),="" cache.load(address3));="" assertequals(this_method,="" "back="" to="" original="" reads",="">
Mar 15, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here